このトピックでは、簡易アップロードの使用方法について説明します。
ファイルをアップロードするために、bucket.put_object メソッドの使用が可能です。 このメソッドは、次の表に示す複数の種類の入力をサポートしています。
型 | アップロード方法 |
---|---|
文字列 | 直接アップロードします。 |
バイト | 直接アップロードします。 |
Unicode | UTF-8 エンコードされたバイトに変換後、アップロードします。 |
ローカルファイル | バイナリモードで開くファイルオブジェクトとしてアップロードします。 |
ネットワークストリーム | Chunked Encoding メソッドで Iterable オブジェクトとしてアップロードします。 |
- 文字列のアップロード
次のコードを実行し、文字列をアップロードします。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>') # Return value. result = bucket.put_object('<yourObjectName>', 'content of object') # HTTP return code. print('http status: {0}'.format(result.status)) # Request ID. The request ID is the unique identification of a request. We strongly recommend you to add this parameter in the program log. print('request_id: {0}'.format(result.request_id)) # Etag is a unique attribute of the value returned by put_object. print('ETag: {0}'.format(result.etag)) # HTTP response header. print('date: {0}'.format(result.headers['date']))
- バイトのアップロード
次のコードを実行し、バイトをアップロードします。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>') bucket.put_object('<yourObjectName>', b'content of object')
- Unicode のアップロード
次のコードを実行し、Unicode をアップロードします。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>') bucket.put_object('<yourObjectName>', u'content of object')
- ローカルファイルのアップロード
次のコードを実行し、ローカルファイルをアップロードします。
# -*- coding: utf-8 -*- import oss2 # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>') # Files must be opened in the binary mode because the number of bytes included in the file is required for upload. with open('<yourLocalFile>', 'rb') as fileobj: # The seek method specifies that the read or write operations start from the 1000th byte. The upload starts from the 1000th byte until the end of the file. fileobj.seek(1000, os.SEEK_SET) # The tell method is used to return to the current location. current = fileobj.tell() bucket.put_object('<yourObjectName>', b'content of object')
OSS Python SDK はローカルファイルをアップロードするためのより便利な方法を提供します:
bucket.put_object_from_file('<yourObjectName>', '<yourLocalFile>')
- ネットワークストリームのアップロード
次のコードを実行してネットワークストリームをアップロードできます:
# -*- coding: utf-8 -*- import oss2 import requests # It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. auth = oss2.Auth('<yourAccessKeyId>', '<yourAccessKeySecret>') # This example uses endpoint China (Hangzhou). Specify the actual endpoint based on your requirements. bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', '<yourBucketName>') # The requests.get method returns an Iterable object, and the Python SDK uploads the network stream in the Chunked Encoding method. input = requests.get('http://www.aliyun.com') bucket.put_object('<yourObjectName>', input)