This topic describes how to use the simple upload method to upload a single file to OSS. This method is simple and is suitable for fast file uploads to cloud storage.
Important
The sample code in this topic uses the region IDcn-hangzhou for China (Hangzhou) as an example. By default, a public endpoint is used. If you want to access OSS from other Alibaba Cloud products in the same region, you must use an internal endpoint. For more information about the mappings between OSS regions and endpoints, 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) → PutObjectResultRequest parameters
Parameter | Type | Description |
request | PutObjectRequest | Specifies the request parameters, such as the access control list (ACL) of the object, whether to forbid overwriting (ForbidOverwrite), and custom metadata (Metadata). For more information, see PutObjectRequest |
Return values
Type | Description |
PutObjectResult | The return value. For more information, see PutObjectResult |
For the complete definition of the simple upload method, see put_object.
Upload a local file
When you upload an object, if an object with the same name already exists in the bucket and you have the required permissions, the new object overwrites the existing one.
The common parameters for uploading a file are as follows:
Parameter | Description |
bucket_name | The bucket name. The naming conventions for buckets are as follows:
|
object_name | The full path of the object. The full path cannot contain the bucket name. The naming conventions for objects are as follows:
|
You can use the put_object_from_file method to upload a local file to the destination bucket.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="put object from file sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the object. This argument is required.
parser.add_argument('--key', help='The name of the object.', required=True)
# Add the --file_path command-line argument, which specifies the path of the local file to upload. This argument is required.
parser.add_argument('--file_path', help='The path of Upload file.', required=True)
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Load credentials from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the software development kit (SDK) and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration.
cfg.region = args.region
# If the endpoint argument is provided, set the endpoint in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client using the specified configurations.
client = oss.Client(cfg)
# Execute the request to upload the 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, # The name of the bucket.
key=args.key # The name of the object.
),
args.file_path # The path of the local file.
)
# Print the result information of the request, including the status code, request ID, Content-MD5, ETag, 64-bit cyclic redundancy check (CRC64) hash, 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")},'
)
# The script entry point. The main function is called when the file is run directly.
if __name__ == "__main__":
main()
When you use the put_object method to upload a local file, you must open the file in 'rb' mode. This ensures that the original byte stream is uploaded instead of text content, which prevents cyclic redundancy check (CRC) failures.
import argparse
import alibabacloud_oss_v2 as oss
# Create a command-line argument parser.
parser = argparse.ArgumentParser(description="put object from file sample")
# Add the --region command-line argument, which specifies the region where the bucket is located. This argument is required.
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
# Add the --bucket command-line argument, which specifies the name of the bucket. This argument is required.
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
# Add the --endpoint command-line argument, which specifies the domain name that other services can use to access OSS. This argument is optional.
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
# Add the --key command-line argument, which specifies the name of the object. This argument is required.
parser.add_argument('--key', help='The name of the object.', required=True)
def main():
# Parse the command-line arguments.
args = parser.parse_args()
# Load credentials from environment variables for identity verification.
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()
# Load the default configurations of the SDK and set the credentials provider.
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
# Set the region in the configuration.
cfg.region = args.region
# If the endpoint argument is provided, set the endpoint in the configuration.
if args.endpoint is not None:
cfg.endpoint = args.endpoint
# Create an OSS client using the specified configurations.
client = oss.Client(cfg)
# Execute the request to upload the 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, # The name of the bucket.
key=args.key, # The name of the object.
body=f.read() # Read the file content.
)
)
# Print the result information of the request, including the status code, request ID, Content-MD5, ETag, CRC64 hash, 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")},'
)
# The script entry point. The main function is called 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.