This topic describes how to quickly upload a local file to Object Storage Service (OSS) using simple upload. This method is straightforward and suitable for scenarios where you need to quickly upload a local file.
Considerations
This example uses the region ID cn-hangzhou
of China (Hangzhou) and the public endpoint by default. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use the internal endpoint. For more information about the mapping between regions and endpoints supported by OSS, see OSS regions and endpoints.
Permissions
By default, an Alibaba Cloud account has full permissions. RAM users or RAM roles under an Alibaba Cloud account do not have any permissions by default. The Alibaba Cloud account or account administrator must grant operation permissions through RAM Policy or Bucket Policy.
API | Action | Definition |
PutObject |
| Uploads an object. |
| When uploading an object, if you specify object tags through x-oss-tagging, this permission is required. | |
| When uploading an object, if the object metadata contains X-Oss-Server-Side-Encryption: KMS, these two permissions are required. | |
|
Method definition
put_object(request: PutObjectRequest, **kwargs) → PutObjectResult
Request parameter list
Parameter name | Type | Description |
request | PutObjectRequest | Set request parameters, such as setting the object access control method (Acl), forbid overwrite (ForbidOverwrite), custom metadata (Metadata), etc. For details, see PutObjectRequest |
Return value list
Type | Description |
PutObjectResult | Return value, for details, see PutObjectResult |
For the complete definition of the simple upload method, see put_object.
Upload local files
If you upload an object that has the same name as an accessible existing object, the existing object will be overwritten by the uploaded object.
The following table lists the common parameters that you must configure when uploading an object.
Parameter | Description |
bucket_name | Bucket name. Bucket names must comply with the following conventions:
|
object_name | Full path of the object. Do not include the bucket name in the full path of the object. Object names must comply with the following conventions:
|
You can use the put_object_from_file
method to directly upload a local file to the target bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser
parser = argparse.ArgumentParser(description="put object from file sample")
# Specify the --region parameter, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter, which specifies the name of the object. This command line parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
# Specify the --file_path parameter, which specifies the path of the local file to upload. This parameter is required.
parser.add_argument('--file_path', help='The path of Upload file.', required=True)
def main():
# Parse the command line parameters
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configuration of the SDK and specify the credential provider
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration to the one specified in the command line
cfg.region = args.region
# If the endpoint parameter is provided, specify the endpoint
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configurations to create an OSSClient instance
client = oss.Client(cfg)
# Execute the request to upload an object directly from a file
# Specify the bucket name, object name, and local file path
result = client.put_object_from_file(
oss.PutObjectRequest(
bucket=args.bucket, # Bucket name
key=args.key # Object name
),
args.file_path # Local file path
)
# Display the result information, including status code, request ID, content MD5, ETag, CRC-64 value, version ID, and server response time
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content md5: {result.content_md5},'
f' etag: {result.etag},'
f' hash crc64: {result.hash_crc64},'
f' version id: {result.version_id},'
f' server time: {result.headers.get("x-oss-server-time")},'
)
# Script entry point, calls the main function when the file is run directly
if __name__ == "__main__":
main()
When using the put_object
method to upload a local file, you must open the file in 'rb'
mode to ensure that the original byte stream is uploaded rather than text content, to avoid cyclic redundancy check failures.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command line argument parser
parser = argparse.ArgumentParser(description="put object from file sample")
# Specify the --region parameter, which specifies the region in which the bucket is located. This parameter is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Specify the --bucket parameter, which specifies the name of the bucket. This command line parameter is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Specify the --endpoint parameter, which specifies the endpoint that other services can use to access OSS. This parameter is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Specify the --key parameter, which specifies the name of the object. This command line parameter is required.
parser.add_argument('--key', help='The name of the object.', required=True)
def main():
# Parse the command line parameters
args = parser.parse_args()
# Obtain access credentials from environment variables for authentication
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configuration of the SDK and specify the credential provider
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration to the one specified in the command line
cfg.region = args.region
# If the endpoint parameter is provided, specify the endpoint
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Use the configurations to create an OSSClient instance
client = oss.Client(cfg)
# Execute the request to upload an object directly from a local file
# Specify the bucket name, object name, and local file path
with open('your-test-file.md', 'rb') as f:
result = client.put_object(
oss.PutObjectRequest(
bucket=args.bucket, # Bucket name
key=args.key, # Object name
body=f.read() # Read file content
)
)
# Display the HTTP status code in the response, the request ID, and the MD5 hash, ETag, CRC-64 value, and version ID of the object to check whether the request is successful
print(f'status code: {result.status_code},'
f' request id: {result.request_id},'
f' content md5: {result.content_md5},'
f' etag: {result.etag},'
f' hash crc64: {result.hash_crc64},'
f' version id: {result.version_id},'
f' server time: {result.headers.get("x-oss-server-time")},'
)
# Script entry point, calls the main function when the file is run directly
if __name__ == "__main__":
main()
Common scenarios
References
For the complete sample code for simple upload, see put_object.py.