oss2 package

Submodules

oss2.api module

Data parameters for file upload methods

For example, put_object has the data parameter. It can be any one of the following types:
  • unicode type (for Python3 it is str): Internally converted to UTF-8 bytes.
  • bytes types: No conversion
  • file-like object: For seekable and tellable file object, it will read from the current position to the end. Otherwise, make sure the current position is the file start position.
  • Iterator types: The data must be of the iterator type if its length is not predictable. Chunked Encoding is used internally for transfer operations.

The input paramater in bucket config update methods

For example put_bucket_cors has the input parameter. It can be any of the following types:
  • Bucket config related class such as BucketCors
  • unicode type (For Python3, it is str)
  • UTF-8 encoded bytes
  • file-like object
  • Iterator types, uses Chunked Encoding for transfer

Except supporting the Bucket configuration related class, the input parameter has the same types as the data parameters.

Return value

Most methods in Service and Bucket return RequestResult or its subclasses. RequestResult class defines the HTTP status code, response headers and OSS Request ID. The subclasses of RequestResult define the specific data that is of interest to users. For example, ListBucketsResult.buckets returns the Bucket instance list. and GetObjectResult returns a file-like object which can call read() to get the response body.

Encrypted interface CryptoBucket :

CryptoBucket provides encrypted interfaces only for upload/download data, such as get_object and put_object. The CryptoBucket return values are the same as for the corresponding Bucket interfaces.

Exceptions

In general, Python SDK can throw up three Exception types, which are inherited from OssError .
  • ClientError : Client side exceptions due to the user’s incorrect usage parameters.
  • ServerError and its subclasses : Server side exceptions which contain error codes such as 4xx or 5xx.
  • RequestError : The underlying requests lib’s exceptions, such as DNS error or timeout.

Besides these, Bucket.put_object_from_file and Bucket.get_object_to_file may throw file-related exceptions.

Download range

For example, get_object and upload_part_copy have the byte_range parameter, which specifies the read range. It is a 2-tuple: (start,last). These methods internally would translate the tuple into the value of Http header Range, such as:

  • For (0, 99), the translated Range header is ‘bytes=0-99’, which means reading the first 100 bytes.
  • For (None, 99), the translated Range header is ‘bytes=-99’, which means reading the last 99 bytes
  • For (100, None), the translated Range header is ‘bytes=100-‘, which means reading the whole data starting from the 101th character (The index is 100 and index starts with 0).

Paging

Lists APIs such as list_buckets and list_objects support paging. Specify the paging markers, for example, marker or key_marker, to query a specific page after that marker. For the first page, the marker is empty, which is the default value. For the following pages, use the next_marker or next_key_marker value as the marker value. Check the is_truncated value after each call to determine if it is the last page. If the value is false, it means it is the last page.

Upload or Download Progress

Upload or Download APIs such as get_object, put_object, resumable_upload support the progress callback method. Users can use it to implement progress bars or other functions when the progress of data is important.

progress_callback definition:
>>> def progress_callback(bytes_consumed, total_bytes):
>>>     '''progress callback
>>> 
>>>     :param int bytes_consumed: Consumed bytes. For upload it's uploaded bytes. for download it is download bytes.
>>>     :param int total_bytes: Total bytes.
>>>     '''
Note that total_bytes has different meanings for download and upload.
  • For upload: If the input is bytes data or file object supports seek/tell, it is the total size. Otherwise it is none.
  • For Download: If http headers that are returned have content-length header, then it is the value of content-length. Otherwise it is none.

Unix Time

OSS Python SDK automatically converts the server time to Unix time (or epoch time, https://en.wikipedia.org/wiki/Unix_time )

The standard time format in OSS is:
  • HTTP Date format, for example Sat, 05 Dec 2015 11:04:39 GMT. It is used in http headers such as If-Modified-Since or Last-Modified.
  • ISO8601 format, for example 2015-12-05T00:00:00.000Z. It is used for lifecycle management configuration, for the create time of a result bucket list, last modified time of a result file list and more.
http_date converts the Unix Time to HTTP Date. http_to_unixtime does the opposite. For example:
>>> import oss2, time
>>> unix_time = int(time.time())             # Current time in UNIX Time, Value is 1449313829
>>> date_str = oss2.http_date(unix_time)     # The date_str will be 'Sat, 05 Dec 2015 11:10:29 GMT'
>>> oss2.http_to_unixtime(date_str)          # The result is 1449313829

Note

Please use http_date instead of strftime to generate the date in http protocol. Because the latter depends on the locale. For example, strftime result could contain Chinese which could not be parsed by OSS server.

iso8601_to_unixtime converts date in ISO8601 format to Unix Time. date_to_iso8601 and iso8601_to_date do the translation between ISO8601 and datetime.date. For example:
>>> import oss2
>>> d = oss2.iso8601_to_date('2015-12-05T00:00:00.000Z')  # Get datetime.date(2015, 12, 5)
>>> date_str = oss2.date_to_iso8601(d)                    # Get '2015-12-05T00:00:00.000Z'
>>> oss2.iso8601_to_unixtime(date_str)                    # Get 1449273600
class oss2.api.Bucket(auth, endpoint, bucket_name, is_cname=False, session=None, connect_timeout=None, app_name='', enable_crc=True)[source]

Bases: oss2.api._Base

The class for Bucket or Object related operations, such as the creation or deletion of buckets, and the upload or download of objects.

Usage (A Bucket in the Hangzhou data center is used as an example):
>>> import oss2
>>> auth = oss2.Auth('your-access-key-id', 'your-access-key-secret')
>>> bucket = oss2.Bucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'your-bucket')
>>> bucket.put_object('readme.txt', 'content of the object')
<oss2.models.PutObjectResult object at 0x029B9930>
Parameters:
  • auth (oss2.Auth) – Auth object that contains the AccessKeyId and AccessKeySecret of the user.
  • endpoint (str) – Domain name of endpoint or the CName.
  • bucket_name (str) – Bucket name
  • is_cname (bool) – True if the endpoint is CNAME. Otherwise, it is False.
  • session (oss2.Session) – Session instance. None if creating a new session.
  • connect_timeout (float) – Connection timeout in seconds.
  • app_name (str) – App name. If it is not empty, the name will be appended in User Agent. Note that this value will be a part of the HTTP Header value and thus must follow http protocol’s format.
ACL = 'acl'
BUCKET_INFO = 'bucketInfo'
COMP = 'comp'
CORS = 'cors'
LIFECYCLE = 'lifecycle'
LIVE = 'live'
LOCATION = 'location'
LOGGING = 'logging'
REFERER = 'referer'
STAT = 'stat'
STATUS = 'status'
VOD = 'vod'
WEBSITE = 'website'
abort_multipart_upload(key, upload_id)[source]

Abort a multipart upload.

Parameters:
  • key (str) – The object key, must be the same as the one in init_multipart_upload().
  • upload_id (str) – The multipart upload ID.
Returns:

RequestResult

append_object(key, position, data, headers=None, progress_callback=None, init_crc=None)[source]

Append the data to an existing object or create a new appendable file if there is no existing object.

Parameters:
  • key (str) – The new file name or existing file name .
  • position (int) – It’s 0 for creating a new appendable file or the current length for appending an existing file. The position value can be got from AppendObjectResult.next_position of the previous append_object results.
  • data (str,bytes,file-like object or iterator object.) – User data
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers. It can be content-type, Content-MD5 or x-oss-meta- prefixed headers.
  • progress_callback – The user’s callback. A typical usage is the progress bar. Check out Upload or Download Progress for more information.
Returns:

AppendObjectResult

Raises:

If the position is not the same as the current file’s length, PositionNotEqualToLength is thrown. If the file is not appendable, ObjectNotAppendable is thrown. Other client side exceptions are also thrown.

batch_delete_objects(key_list)[source]

Delete objects specified in the batch key_list. The key_list cannot be empty.

Parameters:key_list (list of str) – The object key list, non-empty.
Returns:BatchDeleteObjectsResult
complete_multipart_upload(key, upload_id, parts, headers=None)[source]

Complete a multipart upload. A file would be created with all the parts’ data and these parts will not be available to user.

Parameters:
  • key (str) – The object key which should be the same as the one in init_multipart_upload().
  • upload_id (str) – The multipart upload ID.
  • parts (list of PartInfo) – PartInfo list. The part_number and ETag are required in PartInfo. The ETag comes from the result of upload_part().
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
Returns:

PutObjectResult

copy_object(source_bucket_name, source_key, target_key, headers=None)[source]

Copy a file to the current bucket.

Parameters:
  • source_bucket_name (str) – Source bucket name
  • source_key (str) – Source file name
  • target_key (str) – Target file name
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
Returns:

PutObjectResult

create_bucket(permission=None, input=None)[source]

Create a new bucket.

Parameters:
  • permission (str) – Bucket ACL. It could be oss2.BUCKET_ACL_PRIVATE (recommended,default value) or oss2.BUCKET_ACL_PUBLIC_READ or oss2.BUCKET_ACL_PUBLIC_READ_WRITE.
  • inputBucketCreateConfig object
create_live_channel(channel_name, input)[source]

Create a live channel.

Parameters:
  • channel_name (str) – The live channel name.
  • input – LiveChannelInfo instance, which includes the live channel’s description information.
Returns:

CreateLiveChannelResult

delete_bucket()[source]

Delete a bucket. A bucket can be deleted only when the bucket is empty and there are no incomplete multipart uploads.

Returns:RequestResult
Raises:If the bucket is not empty, BucketNotEmpty is thrown.
delete_bucket_cors()[source]

Delete the bucket CORS.

delete_bucket_lifecycle()[source]

Delete the lifecycle of the bucket. It still return 200 OK if the lifecycle does not exist.

delete_bucket_logging()[source]

Delete the bucket’s logging configuration—the existing logging files are not deleted.

delete_bucket_website()[source]

Delete the static website configuration.

delete_live_channel(channel_name)[source]

Delete the live channel.

Parameters:channel_name (str) – The live channel name.
delete_object(key)[source]

Delete a file.

Parameters:key (str) – The object key.
Returns:RequestResult
get_bucket_acl()[source]

Get bucket ACL.

Returns:GetBucketAclResult
get_bucket_cors()[source]

Get the bucket CORS.

Returns:GetBucketCorsResult
get_bucket_info()[source]

Get the bucket info, such as Created-Time, Endpoint, Owner, ACL and more.

Returns:GetBucketInfoResult
get_bucket_lifecycle()[source]

Get the bucket lifecycle.

Returns:GetBucketLifecycleResult
Raises:If the lifecycle is not set in the bucket, NoSuchLifecycle is thrown.
get_bucket_location()[source]

Get the bucket location.

Returns:GetBucketLocationResult
get_bucket_logging()[source]

Get the bucket logging.

Returns:GetBucketLoggingResult
get_bucket_referer()[source]

Get the bucket’s allowed referer.

Returns:GetBucketRefererResult
get_bucket_stat()[source]

Get the bucket status, including bucket size, count of objects, count of ongoing multipart uploads.

Returns:GetBucketStatResult
get_bucket_website()[source]

Get the static website configuration.

Returns:GetBucketWebsiteResult
Raises:If the static website config is not set, NoSuchWebsite is thrown.
get_live_channel(channel_name)[source]

Get the live channel configuration.

Parameters:channel_name (str) – The live channel name.
Returns:GetLiveChannelResult
get_live_channel_history(channel_name)[source]

Retrieve the 10 most recently pushed live channel streams. Each record includes the start time, the end time, and the remote address (the source address of the pushed stream).

Parameters:channel_name (str) – The live channel name.
Returns:GetLiveChannelHistoryResult
get_live_channel_stat(channel_name)[source]

Get the pushed live channel stream status.

Parameters:channel_name (str) – The live channel name.
Returns:GetLiveChannelStatResult
get_object(key, byte_range=None, headers=None, progress_callback=None, process=None, params=None)[source]

Download a file.

Usage:
>>> result = bucket.get_object('readme.txt')
>>> print(result.read())
'hello world'
Parameters:
  • key – The object name in OSS
  • byte_range – Download range. See Download range for more information.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
  • progress_callback – The progress callback function specified by the user. Please see Upload or Download Progress for more information.
  • process – The OSS file process, for example image processing. The process is applied to the returned object.
  • params – Query string parameters for HTTP requests.
Returns:

The file-like object

Raises:

If the file does not exist, NoSuchKey is thrown. Other exceptions may also be thrown.

get_object_acl(key)[source]

Get the object ACL.

Returns:GetObjectAclResult
get_object_meta(key)[source]

Get the object’s basic metadata which includes ETag, Size, LastModified, and it does not return object content.

The metadata is in HTTP response headers, which could be accessed by GetObjectMetaResult’s last_modified, content_length, etag .

Parameters:key – The object key in OSS.
Returns:GetObjectMetaResult
Raises:If file does not exist, NoSuchKey is thrown. Other exceptions can also be thrown.
get_object_to_file(key, filename, byte_range=None, headers=None, progress_callback=None, process=None, params=None)[source]

Download an object to the local file.

Parameters:
  • key – The object name in OSS.
  • filename – The local file name. The parent directory should be existent and have write permissions.
  • byte_range – The download range. See Download range.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers.
  • progress_callback – The progress callback function specified by the user. See Upload or Download Progress for more infomation.
  • process – The OSS file process, for example image processing. The process is applied to the returned object.
Returns:

If the file does not exist, NoSuchKey is thrown. Other exceptions may also be thrown.

Get the symbolic file’s information.

Parameters:symlink_key (str) – The symbolic file key.
Returns:GetSymlinkResult
Raises:If the symbolic file does not exist, NoSuchKey is thrown. If the key is the symbolic file, then ServerError with error code NotSymlink is returned. Other exceptions may also be thrown.
head_object(key, headers=None)[source]

Get object metadata.

The metadata is in HTTP response headers, which could be accessed by RequestResult.headers.

Usage:
>>> result = bucket.head_object('readme.txt')
>>> print(result.content_type)
text/plain
Parameters:
  • key – The object name in OSS.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers.
Returns:

HeadObjectResult

Raises:

If the bucket or file does not exist, NotFound is thrown.

init_multipart_upload(key, headers=None)[source]

Initialize a multipart upload.

upload_id, bucket name and object key in the returned value forms a 3-tuple which is a unique ID for the upload.

Parameters:
  • key (str) – The object key.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers.
Returns:

InitMultipartUploadResult

list_live_channel(prefix='', marker='', max_keys=100)[source]

List all live channels under the bucket according to the prefix and marker filters

Parameters:
  • prefix (str) – The channel ID must start with this prefix.
  • marker (str) – The channel ID marker for paging.
  • max_keys (int) – The maximum channel count to return.
Returns:

ListLiveChannelResult

list_multipart_uploads(prefix='', delimiter='', key_marker='', upload_id_marker='', max_uploads=1000)[source]

Lists all the ongoing multipart uploads. It supports paging.

Parameters:
  • prefix (str) – The prefix filter.
  • delimiter (str) – The delimiter of folder.
  • key_marker (str) – The key marker for paging. It is empty for first page and then the next_key_marker is used in the response of the previous page.
  • upload_id_marker (str) – The upload ID marker for paging. It is empty for first page, and then the next_upload_id_marker in the response of the previous page.
  • max_uploads (int) – Max entries to return.
Returns:

ListMultipartUploadsResult

list_objects(prefix='', delimiter='', marker='', max_keys=100)[source]

List objects by the prefix under a bucket.

Parameters:
  • prefix (str) – The prefix of the objects to list.
  • delimiter (str) – The folder separator which can simulate directory.
  • marker (str) – Paging marker. It’s empty for first page and then use next_marker in the response of the previous page.
  • max_keys (int) – Max entries to return. The sum of files and directories should not exceed that value.
Returns:

ListObjectsResult

list_parts(key, upload_id, marker='', max_parts=1000)[source]

List uploaded parts and it supports paging. As comparison, list_multipart_uploads lists ongoing parts.

Parameters:
  • key (str) – The object key.
  • upload_id (str) – The upload ID.
  • marker (str) – The key marker for paging.
  • max_parts (int) – Max entries to return.
Returns:

ListPartsResult

object_exists(key)[source]

If the file exists, it returns True. Otherwise, it returns False. If the bucket does not exist or other errors occur, exceptions will be thrown.

post_vod_playlist(channel_name, playlist_name, start_time=0, end_time=0)[source]

Generate a VOD play list according to the play list name, start time and end time.

Parameters:
  • channel_name (str) – The live channel name.
  • playlist_name (str) – The playlist name (*.m3u8 file).
  • start_time (int) – Start time in Unix Time, which can be obtained from int(time.time())
  • end_time (int) – End time in Unix Time, which can be obtained from int(time.time())
put_bucket_acl(permission)[source]

Set the bucket ACL.

Parameters:permission (str) – The new ACL whose value could be oss2.BUCKET_ACL_PRIVATE, oss2.BUCKET_ACL_PUBLIC_READ or oss2.BUCKET_ACL_PUBLIC_READ_WRITE
put_bucket_cors(input)[source]

Set the bucket CORS.

Parameters:inputBucketCors instance or data can be converted to BucketCors by xml_utils.to_put_bucket_cors .
put_bucket_lifecycle(input)[source]

Set the lifecycle of the bucket.

Parameters:inputBucketLifecycle instance or data can be converted to BucketLifecycle by xml_utils.to_put_bucket_lifecycle.
put_bucket_logging(input)[source]

Set the bucket logging.

Parameters:inputBucketLogging instance or other data that can be converted to BucketLogging by xml_utils.to_put_bucket_logging .
put_bucket_referer(input)[source]

Set the bucket’s allowed referer.

Parameters:inputBucketReferer instance or other data that can be converted to BucketReferer by xml_utils.to_put_bucket_referer.
put_bucket_website(input)[source]

Set the static website configuration for the bucket.

Parameters:inputBucketWebsite
put_live_channel_status(channel_name, status)[source]

Set the live channel status. Valid statuses are: ‘enabled’ and ‘disabled’.

Parameters:
  • channel_name (str) – The live channel name.
  • status (str) – The live channel’s desired status.
put_object(key, data, headers=None, progress_callback=None)[source]

Upload a normal file (not appendable).

Usage:
>>> bucket.put_object('readme.txt', 'content of readme.txt')
>>> with open(u'local_file.txt', 'rb') as f:
>>>     bucket.put_object('remote_file.txt', f)
Upload a folder
>>> bucket.enable_crc = False # this is needed as by default crc is enabled and it will not work when creating folder.
>>> bucket.put_object('testfolder/', None)
Parameters:
  • key – The object name in OSS.
  • data (bytes, str or file-like object.) – The file content for upload.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers. It can be Content-type, Content-MD5 or x-oss-meta- prefixed headers.
  • progress_callback – The user’s callback. A typical usage is the progress bar. See Upload or Download Progress for more information.
Returns:

PutObjectResult

put_object_acl(key, permission)[source]

Set the object ACL.

Parameters:
  • key (str) – The object name
  • permission (str) – The valid values are oss2.OBJECT_ACL_DEFAULT,oss2.OBJECT_ACL_PRIVATE,oss2.OBJECT_ACL_PUBLIC_READ or oss2.OBJECT_ACL_PUBLIC_READ_WRITE.
Returns:

RequestResult

put_object_from_file(key, filename, headers=None, progress_callback=None)[source]

Upload a normal local file to OSS.

Parameters:
  • key (str) – The object name in oss.
  • filename (str) – The local file path with the read permission.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – The HTTP headers. It could be content-type, Content-MD5 or x-oss-meta- prefixed headers.
  • progress_callback – The user’s callback. Typical usage is progress bar. See Upload or Download Progress for more information.
Returns:

PutObjectResult

Create a symbolic file.

Parameters:
  • target_key (str) – Target file, which cannot be another symbolic file.
  • symlink_key (str) – The symbolic file name. It is a special file whose content points to the target key.
Returns:

RequestResult

restore_object(key)[source]

Restore an object:

If the interface is invoked for the first time for the object, it returns RequestResult.status = 202 . If the restore interface has been successfully invoked and the service-side is still unfrozen, the exception RestoreAlreadyInProgress(status=409) will be thrown. If the restore interface has been successfully invoked and the server-side thawing has been completed, it returns RequestResult.status = 200 when the interface is invoked again, and the object can be downloaded If object does not exist, the exception NoSuchKey (status=404) is thrown. If you invoke the restore interface for non Archive type Object, the exception OperationNotSupported (status=400) is thrown. You can also get the meta data by calling the head_object interface to determine whether you can restore, or to check the status of a restore.

Usage:
>>> meta = bucket.head_object(key)
>>> if meta.resp.headers['x-oss-storage-class'] == oss2.BUCKET_STORAGE_CLASS_ARCHIVE:
>>>     bucket.restore_object(key)
>>>         while True:
>>>             meta = bucket.head_object(key)
>>>             if meta.resp.headers['x-oss-restore'] == 'ongoing-request="true"':
>>>                 time.sleep(5)
>>>             else:
>>>                 break
Parameters:key (str) – The object name
Returns:RequestResult
sign_rtmp_url(channel_name, playlist_name, expires)[source]

Sign RTMP pushed streaming URL.

It is used to push the RTMP streaming to OSS for trusted user who has the URL.

Parameters:
  • channel_name – The live channel name
  • expires – Expiration time in seconds.The URL is invalid after it expires.
  • playlist_name – Playlist name. It should be the one created in live channel creation time.
  • params – HTTP query parameters to sign.
Returns:

Signed URL.

sign_url(method, key, expires, headers=None, params=None)[source]

Generate the presigned URL.

The signed URL can be used to access the object by any user who has the URL. For example, in the code below, it generates the signed URL with 5 minutes TTL for log.jpg file:
>>> bucket.sign_url('GET', 'log.jpg', 5 * 60)
'http://your-bucket.oss-cn-hangzhou.aliyuncs.com/logo.jpg?OSSAccessKeyId=YourAccessKeyId\&Expires=1447178011&Signature=UJfeJgvcypWq6Q%2Bm3IJcSHbvSak%3D'
Parameters:
  • method (str) – HTTP method such as ‘GET’, ‘PUT’, ‘DELETE’, and so on.
  • key – The object key.
  • expires – Expiration time in seconds. The URL is invalid after it expires.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – The HTTP headers to sign. For example, the headers starting with x-oss-meta- (user’s custom metadata), Content-Type, and so on. There is no need to download them.
  • params – HTTP query parameters to sign
Returns:

Signed URL.

update_object_meta(key, headers)[source]

Update Object’s metadata information, including HTTP standard headers such as Content-Type or x-oss-meta- prefixed custom metadata. If user specifies invalid headers, for example non-standard headers or non x-oss-meta- headers, the call would still succeed but no operation is done on the server side.

User could call head_object() to get the updated information. Note that get_object_meta does return all metadata, but head_object does.

Parameters:
  • key (str) – object key
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
Returns:

RequestResult

upload_part(key, upload_id, part_number, data, progress_callback=None, headers=None)[source]

Upload a part.

Parameters:
  • key (str) – The object key to upload. It must be the same as the one in init_multipart_upload().
  • upload_id (str) – The multipart upload ID
  • part_number (int) – The part number, starting with 1.
  • data – Data to upload
  • progress_callback – The the progress callback function specified by the user. Can be used to implement progress bars and other functions. See Upload or Download Progress for more information.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers, such as Content-MD5 .
Returns:

PutObjectResult

upload_part_copy(source_bucket_name, source_key, byte_range, target_key, target_upload_id, target_part_number, headers=None)[source]

Uploads a part from another object. Copy part or the whole of an existing file into a slice of the target file.

Parameters:
  • byte_range – The range to copy in the source file Download range
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
Returns:

PutObjectResult

class oss2.api.CryptoBucket(auth, endpoint, bucket_name, crypto_provider, is_cname=False, session=None, connect_timeout=None, app_name='', enable_crc=True)[source]

Class to encrypt the operation of Bucket and Object, such as upload/download object. Create/Delete bucket operations need to use the interface of Class Bucket.

Usage (Suppose Bucket belongs to the Hangzhou region):
>>> import oss2
>>> auth = oss2.Auth('your-access-key-id', 'your-access-key-secret')
>>> bucket = oss2.CryptoBucket(auth, 'http://oss-cn-hangzhou.aliyuncs.com', 'your-bucket', oss2.LocalRsaProvider())
>>> bucket.put_object('readme.txt', 'content of the object')
<oss2.models.PutObjectResult object at 0x029B9930>
Parameters:
  • auth (oss2.Auth) – It is an oss2.Auth object containing access-key-id and access-key-secret.
  • endpoint (str) – Domain name of endpoint or the CName.
  • bucket_name (str) – Bucket name.
  • crypto_provider (oss2.crypto.LocalRsaProvider) – The client encryption class, The parameter is empty by default.
  • is_cname (bool) – True if the endpoint is CNAME. Otherwise, it is False.
  • session (oss2.Session) – Session instance. None for creating a new session, if it is not None it will resuse the session.
  • connect_timeout (float) – Connection timeout in seconds.
  • app_name (str) – App name. If it is not empty, it is appended in User Agent. Note that this value will be part of the HTTP Header value and thus must follow http protocol’s format.
  • enable_crc (bool) – True if you want to enable the CRC check. Otherwise it is False.
get_object(key, headers=None, progress_callback=None, params=None)[source]

Download a file.

Usage:
>>> result = bucket.get_object('readme.txt')
>>> print(result.read())
'hello world'
Parameters:
  • key – The object name in OSS.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers.
  • progress_callback – User callback. See Upload or Download Progress for more information.
Returns:

The file-like object.

Raises:

If the file does not exist, NoSuchKey is thrown. Other exceptions may also be thrown.

get_object_to_file(key, filename, headers=None, progress_callback=None, params=None)[source]

Download a file to the local folder.

Parameters:
  • key – The object name in OSS.
  • filename – Local file name. The folder of the file must be available for write and must be pre-existing.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers
  • progress_callback – User callback. See Upload or Download Progress for more information.
  • process – OSS file process, for example, image processing. The process is applied to the returned object.
Returns:

If the file does not exist, NoSuchKey is thrown. Other exceptions may also be thrown.

put_object(key, data, headers=None, progress_callback=None)[source]

Upload a normal file.

Usage:
>>> bucket.put_object('readme.txt', 'content of readme.txt')
>>> with open(u'local_file.txt', 'rb') as f:
>>>     bucket.put_object('remote_file.txt', f)
Parameters:
  • key – The object name in OSS.
  • data (bytes, str or file-like object) – The content for upload.
  • headers (dict or oss2.CaseInsensitiveDict(recommended)) – HTTP headers. It can be content-type, Content-MD5 or x-oss-meta- prefixed headers.
  • progress_callback – The user’s callback. Typical usage is the progress bar. See Upload or Download Progress for more information.
Returns:

PutObjectResult

put_object_from_file(key, filename, headers=None, progress_callback=None)[source]

Upload a normal object from a local file to OSS.

Parameters:
  • key (str) – The object name in OSS.
  • filename (str) – Local file path, called needs the read permission.
  • headers (dict, but recommendation is oss2.CaseInsensitiveDict) – HTTP headers. It could be content-type, Content-MD5 or x-oss-meta- prefixed headers.
  • progress_callback – The user’s callback. A typical usage is the progress bar. See Upload or Download Progress for more information.
Returns:

PutObjectResult

class oss2.api.Service(auth, endpoint, session=None, connect_timeout=None, app_name='')[source]

Bases: oss2.api._Base

The class for interacting with Service related operations, such as listing all buckets.

Usage:
>>> import oss2
>>> auth = oss2.Auth('your-access-key-id', 'your-access-key-secret')
>>> service = oss2.Service(auth, 'oss-cn-hangzhou.aliyuncs.com')
>>> service.list_buckets()
<oss2.models.ListBucketsResult object at 0x0299FAB0>
Parameters:
  • auth (oss2.Auth) – The auth instance that contains access-key-id and access-key-secret.
  • endpoint (str) – The endpoint domain, such as ‘oss-cn-hangzhou.aliyuncs.com’.
  • session (oss2.Session) – The session instance. If specified as None, a new session is used. Otherwise, it will reuse the previous session.
  • connect_timeout (float) – The connection timeout in seconds.
  • app_name (str) – App name. If this is not empty, it will be appended in the User Agent header. Note that this value will be a part of the HTTP Header value and thus must follow http protocol’s format.
list_buckets(prefix='', marker='', max_keys=100)[source]

List buckets by prefix

Parameters:
  • prefix (str) – List buckets with the prefix. List all buckets if it’s empty.
  • marker (str) – The paging maker. It’s empty for first page and then use next_marker in the response of the previous page.
  • max_keys (int) – Max bucket count to return.
Returns:

The bucket list

Return type:

ListBucketsResult

oss2.auth module

class oss2.auth.AnonymousAuth[source]

Bases: object

Anonymous Auth

Note

Anonymous users can only read buckets with public-read permissions, or read from or write to buckets with public-read-write permissions. They are unable to execute service or bucket related operations, such as listing objects under a bucket.

class oss2.auth.Auth(access_key_id, access_key_secret)[source]

Bases: oss2.auth.AuthBase

The first version of the signature. Stores AccessKeyId and AccessKeySecret of the user, and calculates the signature.

class oss2.auth.AuthBase(access_key_id, access_key_secret)[source]

Bases: object

Store user’s AccessKeyId,AccessKeySecret information and calcualte the signature.

class oss2.auth.AuthV2(access_key_id, access_key_secret)[source]

Bases: oss2.auth.AuthBase

The signature version 2. The differences from version 1 are below : 1. Using the SHA256 algorithm, it has higher security. 2. Parameter calculation contains all HTTP query parameters.

class oss2.auth.StsAuth(access_key_id, access_key_secret, security_token, auth_version='v1')[source]

Bases: object

Used for STS authentication. Users can get the AccessKeyID, AccessKeySecret, and SecurityToken from the Alibaba Cloud STS service (https://aliyuncs.com).

Note

The AccessKeyId/Secret and SecurityToken have expiration times. When they are renewed, the STSAuth property of class Bucket instance must be updated with the new credentials.

Parameters:
  • access_key_id (str) – Temporary AccessKeyId
  • access_key_secret (str) – Temporary AccessKeySecret
  • security_token (str) – Temporary SecurityToken
  • auth_version (str) – The version of the auth needs to be generated, the default value is AUTH_VERSION_1(v1).
oss2.auth.make_auth(access_key_id, access_key_secret, auth_version='v1')[source]
oss2.auth.v2_uri_encode(raw_text)[source]

oss2.compat module

Compatible Python versions

oss2.compat.stringify(input)[source]
oss2.compat.to_bytes(data)[source]

Covert to UTF-8 encoding if the input is unicode; otherwise return the original data.

oss2.compat.to_string(data)[source]

Convert to str object

oss2.compat.to_unicode(data)[source]

Convert the input to unicode if it’s utf-8 bytes.

oss2.crypto module

oss2.encryption

The module contains functions and classes related to client-side encryption and decryption.

class oss2.crypto.AliKMSProvider(access_key_id, access_key_secret, region, cmkey, sts_token=None, passphrase=None, cipher=<class oss2.utils.AESCipher>)[source]

Bases: oss2.crypto.BaseCryptoProvider

Use the aliyun kms service to encrypt the data key. The detailed description of KMS please refer to https://help.aliyun.com/product/28933.html?spm=a2c4g.11186623.3.1.jlYT4v
This interface is not available if the Python version is less than py3.3. Refer to https://github.com/aliyun/aliyun-openapi-python-sdk/issues/61 for more information.
Parameters:
  • access_key_id (str) – The access_key_id to visit the KMS key service.
  • access_key_secret (str) – The access_key_secret to visit the KMS key service.
  • region (str) – The kms key service region.
  • cmkey (str) – User master key.
  • sts_token (str) – The security token which you need to provide if you use temporary AK.
  • passphrase (str) – Kms key service password.
  • cipher (class) – Data encryption, default is aes256, currently supports only the default implementation.
build_header(headers=None)[source]
decrypt_oss_meta_data(headers, key, conv=<function <lambda>>)[source]
get_key()[source]
get_start()[source]
class oss2.crypto.BaseCryptoProvider(cipher)[source]

Bases: object

CryptoProvider base class provides an encryption and decryption adapter for basic data.

make_decrypt_adapter(stream, key, start)[source]
make_encrypt_adapter(stream, key, start)[source]
class oss2.crypto.LocalRsaProvider(dir=None, key='', passphrase=None, cipher=<class oss2.utils.AESCipher>)[source]

Bases: oss2.crypto.BaseCryptoProvider

Use the local RSA to encrypt the data key.

Parameters:
  • dir (str) – The storage path of local RSA public key and private key.
  • key (str) – The prefix of local RSA public and private key name.
  • passphrase (str) – The password of local RSA public key and private key.
  • cipher (class) – Data encryption. It is aes256 by default. Users can achieve their own symmetric encryption algorithm, but need to comply with the AESCipher annotation rules.
PRIV_KEY_FILE = '.private_key.pem'
PUB_KEY_FILE = '.public_key.pem'
build_header(headers=None)[source]
decrypt_oss_meta_data(headers, key, conv=<function <lambda>>)[source]
get_key()[source]
get_start()[source]

oss2.defaults module

oss2.defaults

Global Default variables.

oss2.defaults.connect_timeout = 60

Connection timeout

oss2.defaults.connection_pool_size = 10

Connection pool size for each session.

oss2.defaults.get(value, default_value)[source]
oss2.defaults.get_logger()[source]
oss2.defaults.logger = <logging.RootLogger object>

Default Logger

oss2.defaults.multiget_num_threads = 4

Default thread count for multipart download (multiget)

oss2.defaults.multiget_part_size = 10485760

Default part size in multipart download (multiget)

oss2.defaults.multiget_threshold = 104857600

The threshold of file size for using multipart download (multiget) in some APIs.

oss2.defaults.multipart_num_threads = 1

Default thread count for multipart upload.

oss2.defaults.multipart_threshold = 10485760

The threshold of file size for using multipart upload in some APIs.

oss2.defaults.part_size = 10485760

Default part size.

oss2.defaults.request_retries = 3

Retry count

oss2.exceptions module

oss2.exceptions

Exception classes

exception oss2.exceptions.AccessDenied(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'AccessDenied'
status = 403
exception oss2.exceptions.BucketNotEmpty(status, headers, body, details)[source]

Bases: oss2.exceptions.Conflict

code = 'BucketNotEmpty'
status = 409
exception oss2.exceptions.ChannelStillLive(status, headers, body, details)[source]

Bases: oss2.exceptions.Conflict

code = 'ChannelStillLive'
status = 409
exception oss2.exceptions.ClientError(message)[source]

Bases: oss2.exceptions.OssError

exception oss2.exceptions.Conflict(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = ''
status = 409
exception oss2.exceptions.InconsistentError(message, request_id='')[source]

Bases: oss2.exceptions.OssError

exception oss2.exceptions.InvalidArgument(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'InvalidArgument'
status = 400
exception oss2.exceptions.InvalidDigest(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'InvalidDigest'
status = 400
exception oss2.exceptions.InvalidObjectName(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'InvalidObjectName'
status = 400
exception oss2.exceptions.InvalidRequest(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'InvalidRequest'
status = 400
exception oss2.exceptions.LiveChannelDisabled(status, headers, body, details)[source]

Bases: oss2.exceptions.Conflict

code = 'LiveChannelDisabled'
status = 409
exception oss2.exceptions.MalformedXml(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'MalformedXML'
status = 400
exception oss2.exceptions.NoSuchBucket(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchBucket'
status = 404
exception oss2.exceptions.NoSuchCors(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchCORSConfiguration'
status = 404
exception oss2.exceptions.NoSuchKey(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchKey'
status = 404
exception oss2.exceptions.NoSuchLifecycle(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchLifecycle'
status = 404
exception oss2.exceptions.NoSuchLiveChannel(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchLiveChannel'
status = 404
exception oss2.exceptions.NoSuchUpload(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchUpload'
status = 404
exception oss2.exceptions.NoSuchWebsite(status, headers, body, details)[source]

Bases: oss2.exceptions.NotFound

code = 'NoSuchWebsiteConfiguration'
status = 404
exception oss2.exceptions.NotFound(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = ''
status = 404
exception oss2.exceptions.NotModified(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = ''
status = 304
exception oss2.exceptions.ObjectNotAppendable(status, headers, body, details)[source]

Bases: oss2.exceptions.Conflict

code = 'ObjectNotAppendable'
status = 409
exception oss2.exceptions.OpenApiFormatError(message)[source]

Bases: oss2.exceptions.OssError

exception oss2.exceptions.OpenApiServerError(status, request_id, message, error_code)[source]

Bases: oss2.exceptions.OssError

exception oss2.exceptions.OperationNotSupported(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'OperationNotSupported'
status = 400
exception oss2.exceptions.OssError(status, headers, body, details)[source]

Bases: exceptions.Exception

body = None

HTTP response body

code = None

OSS error code

details = None

The error messages. It is a dict of <string, string>

request_id = None

Request ID, which represents a unique OSS request. It is useful when submitting a support ticket.

status = None

HTTP Status code (such as 200)

exception oss2.exceptions.PositionNotEqualToLength(status, headers, body, details)[source]

Bases: oss2.exceptions.Conflict

code = 'PositionNotEqualToLength'
status = 409
exception oss2.exceptions.PreconditionFailed(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'PreconditionFailed'
status = 412
exception oss2.exceptions.RequestError(e)[source]

Bases: oss2.exceptions.OssError

exception oss2.exceptions.RestoreAlreadyInProgress(status, headers, body, details)[source]

Bases: oss2.exceptions.ServerError

code = 'RestoreAlreadyInProgress'
status = 409
exception oss2.exceptions.ServerError(status, headers, body, details)[source]

Bases: oss2.exceptions.OssError

oss2.exceptions.klass

alias of oss2.exceptions.AccessDenied

oss2.exceptions.make_exception(resp)[source]

oss2.http module

oss2.http

This is the HTTP Adapters for requests library. So that the dependency of request library is totally transparent to the SDK caller. It has the wrapper class Session, Request, Response for its counterparts in the requests library.

class oss2.http.Request(method, url, data=None, params=None, headers=None, app_name='')[source]

Bases: object

class oss2.http.Response(response)[source]

Bases: object

read(amt=None)[source]
class oss2.http.Session[source]

Bases: object

Requests of the same session share the same connection pool and possibly the same HTTP connection.

do_request(req, timeout)[source]

oss2.iterators module

oss2.iterators

This module contains some easy-to-use iterators for enumerating bucket, file, parts and more.

class oss2.iterators.BucketIterator(service, prefix='', marker='', max_keys=100, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator for bucket.

Returns a SimplifiedBucketInfo instance for each iteration (via next()).

Parameters:
  • serviceService instance .
  • prefix – Bucket name prefix. Only buckets with the prefix specified are listed.
  • marker – Paging marker. Only lists buckets whose name is after the marker in the lexicographic order.
  • max_keys – The maximum number of keys to return for list_objects . Note that the total entries the iterator returns can be more than that.
class oss2.iterators.LiveChannelIterator(bucket, prefix='', marker='', max_keys=100, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator of Live Channel in a bucket.

It returns a LiveChannelInfo instance for each iteration.

Parameters:
  • bucketBucket instance.
  • prefix – Live Channel prefix.
  • marker – Paging marker.
  • max_keys – Max entries for each list_live_channel call. Note that the total entries that the iterator returns can be more than this value.
class oss2.iterators.MultipartUploadIterator(bucket, prefix='', delimiter='', key_marker='', upload_id_marker='', max_uploads=1000, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator of ongoing parts in multiparts upload.

Returns a MultipartUploadInfo instance for each iteration. When MultipartUploadInfo.is_prefix() is True, the instance is a folder. Otherwise it is a file.

Parameters:
  • bucketBucket instance.
  • prefix – The file key prefix. Only parts of those files will be listed.
  • delimiter – The directory delimeter.
  • key_marker – Paging marker.
  • upload_id_marker – Paging upload ID marker.
  • max_uploads – Maximum entries for each list_multipart_uploads call. Note that the total entries that the iterator returns can be more than this value.
class oss2.iterators.ObjectIterator(bucket, prefix='', delimiter='', marker='', max_keys=100, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator for files in a bucket.

Returns a SimplifiedObjectInfo instance for each iteration (via next()). When SimplifiedObjectInfo.is_prefix() is True, the object is a common prefix (directory, not a file); Otherwise, it is a file.

Parameters:
  • bucketBucket instance.
  • prefix – The file name prefix.
  • delimiter – Delimiter for the directory.
  • marker – Paging marker.
  • max_keys – The maximum number of keys to return for each list_objects call. Note that the total entries that the iterator returns can be more than this value.
class oss2.iterators.ObjectUploadIterator(bucket, key, max_uploads=1000, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator of ongoing multiparts uploads.

It returns a MultipartUploadInfo instance for each iteration. When MultipartUploadInfo.is_prefix() is true, it means the common prefix (folder).

Parameters:
  • bucketBucket instance.
  • key – The object key.
  • max_uploads – Max entries for each list_multipart_uploads call. Note that the total entries the iterator returns can be more than this value.
class oss2.iterators.PartIterator(bucket, key, upload_id, marker='0', max_parts=1000, max_retries=None)[source]

Bases: oss2.iterators._BaseIterator

Iterator of uploaded parts of a specific multipart upload.

It returns a PartInfo instance for each iteration.

Parameters:
  • bucketBucket instance.
  • key – Object key.
  • upload_id – Upload ID.
  • marker – Paging marker.
  • max_parts – The max parts for each list_parts call. Note that the total entries the iterator returns can be more than this value.

oss2.models module

oss2.models

The module contains all classes’ definitions for parameters and returns values in the Python SDK API.

class oss2.models.AbortMultipartUpload(days=None, created_before_date=None)[source]

Bases: object

Abort multipart uploads.

Parameters:
  • days – Delete multipart upload parts whose last modified time is ‘days’ (days in the past).
  • created_before_date – Delete multipart upload parts whose last modified time is earlier than created_before_date.
class oss2.models.AccessControlList(grant)[source]

Bases: object

class oss2.models.AppendObjectResult(resp)[source]

Bases: oss2.models.RequestResult

crc = None

The updated CRC64 value after the append operation.

etag = None

HTTP ETag

next_position = None

The next position for append operation.

class oss2.models.BatchDeleteObjectsResult(resp)[source]

Bases: oss2.models.RequestResult

deleted_keys = None

The deleted file name list

class oss2.models.BucketCors(rules=None)[source]

Bases: object

class oss2.models.BucketCreateConfig(storage_class)[source]

Bases: object

class oss2.models.BucketInfo(name=None, owner=None, location=None, storage_class=None, intranet_endpoint=None, extranet_endpoint=None, creation_date=None, acl=None)[source]

Bases: object

class oss2.models.BucketLifecycle(rules=None)[source]

Bases: object

The bucket’s lifecycle configuration.

Parameters:rules (list of LifecycleRule) – Lifecycle rule list.
class oss2.models.BucketLogging(target_bucket, target_prefix)[source]

Bases: object

Bucket logging configuration.

Parameters:
  • target_bucket (str) – The logging files’ bucket。
  • target_prefix (str) – The prefix of the logging files.
class oss2.models.BucketReferer(allow_empty_referer, referers)[source]

Bases: object

Bucket referer settings.

Parameters:
  • allow_empty_referer (bool) – Flag of allowing empty Referer.
  • referers – Referer list. The type of element is str.
class oss2.models.BucketStat(storage_size_in_bytes, object_count, multi_part_upload_count)[source]

Bases: object

class oss2.models.BucketWebsite(index_file, error_file)[source]

Bases: object

Static website configuration.

Parameters:
  • index_file (str) – The home page file.
  • error_file (str) – 404 not found file.
class oss2.models.CorsRule(allowed_origins=None, allowed_methods=None, allowed_headers=None, expose_headers=None, max_age_seconds=None)[source]

Bases: object

CORS (cross origin resource sharing) rules

Parameters:
  • allowed_origins (list of str) – Allow origins to access the bucket.
  • allowed_methods (list of str) – Allowed HTTP methods for CORS.
  • allowed_headers (list of str) – Allowed HTTP headers for CORS.
class oss2.models.CreateLiveChannelResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.LiveChannelInfo

class oss2.models.GetBucketAclResult(resp)[source]

Bases: oss2.models.RequestResult

acl = None

Bucket ACL, the value could be BUCKET_ACL_PRIVATE, BUCKET_ACL_PUBLIC_READ, BUCKET_ACL_PUBLIC_READ_WRITE .

class oss2.models.GetBucketCorsResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketCors

class oss2.models.GetBucketInfoResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketInfo

class oss2.models.GetBucketLifecycleResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketLifecycle

class oss2.models.GetBucketLocationResult(resp)[source]

Bases: oss2.models.RequestResult

location = None

Bucket’s datacenter location.

class oss2.models.GetBucketLoggingResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketLogging

class oss2.models.GetBucketRefererResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketReferer

class oss2.models.GetBucketStatResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketStat

class oss2.models.GetBucketWebsiteResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.BucketWebsite

class oss2.models.GetLiveChannelHistoryResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.LiveChannelHistory

class oss2.models.GetLiveChannelResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.LiveChannelInfo

class oss2.models.GetLiveChannelStatResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.LiveChannelStat

class oss2.models.GetObjectAclResult(resp)[source]

Bases: oss2.models.RequestResult

acl = None

File ACL, The value could be OBJECT_ACL_DEFAULT, OBJECT_ACL_PRIVATE, OBJECT_ACL_PUBLIC_READ or OBJECT_ACL_PUBLIC_READ_WRITE

class oss2.models.GetObjectMetaResult(resp)[source]

Bases: oss2.models.RequestResult

content_length = None

Content-Length,file size in bytes. Type is int.

etag = None

HTTP ETag

last_modified = None

Last modified time of a file, in Unix time. See Unix Time for more information.

class oss2.models.GetObjectResult(resp, progress_callback=None, crc_enabled=False, crypto_provider=None)[source]

Bases: oss2.models.HeadObjectResult

client_crc
read(amt=None)[source]
server_crc
class oss2.models.GetSymlinkResult(resp)[source]

Bases: oss2.models.RequestResult

target_key = None

The target file of the symlink file.

class oss2.models.HeadObjectResult(resp)[source]

Bases: oss2.models.RequestResult

content_length = None

Content-Length,it could be None。

content_type = None

File’s MIME type.

etag = None

HTTP ETag

object_type = None

File type, three types are supported: ‘Normal’,’Multipart’,’Appendable’.

class oss2.models.InitMultipartUploadResult(resp)[source]

Bases: oss2.models.RequestResult

upload_id = None

Initial Upload ID

class oss2.models.LifecycleExpiration(days=None, date=None, created_before_date=None)[source]

Bases: object

Life cycle expiration.

Parameters:
  • days – The days after last modified to trigger the expiration rule (such as delete files).
  • date (datetime.date) – The date threshold to trigger the expiration rule—after this date the expiration rule. After this date the expiration rule will always be valid (not recommended).
  • created_before_date – Delete files if their last modified time is earlier than created_before_date
class oss2.models.LifecycleRule(id, prefix, status='Enabled', expiration=None, abort_multipart_upload=None, storage_transitions=None)[source]

Bases: object

Life cycle rule.

Parameters:
  • id – Rule name.
  • prefix – File prefix to match the rule.
  • expiration (LifecycleExpiration) – Expiration time.
  • status – Enable or disable the rule. The value is either LifecycleRule.ENABLED or LifecycleRule.DISABLED
DISABLED = 'Disabled'
ENABLED = 'Enabled'
class oss2.models.ListBucketsResult(resp)[source]

Bases: oss2.models.RequestResult

buckets = None

Get the bucket list. The type is SimplifiedBucketInfo.

is_truncated = None

True means more buckets to list; False means all buckets have been listed.

next_marker = None

The next paging marker. It can be the value of a parameter marker in list_buckets.

class oss2.models.ListLiveChannelResult(resp)[source]

Bases: oss2.models.RequestResult, oss2.models.LiveChannelList

class oss2.models.ListMultipartUploadsResult(resp)[source]

Bases: oss2.models.RequestResult

is_truncated = None

True means there are more ongoing multiparts uploads to list. False means there are no more multiparts uploads to list.

next_key_marker = None

The paging key marker.

next_upload_id_marker = None

The paging upload Id marker

prefix_list = None

The common prefix. The type is str.

upload_list = None

The list of the multiparts upload. The type is MultipartUploadInfo.

class oss2.models.ListObjectsResult(resp)[source]

Bases: oss2.models.RequestResult

is_truncated = None

True means there are more files to list. False means all files are listed.

next_marker = None

Paging marker for next call. It should be the value of parameter marker in next list_objects call.

object_list = None

The object list. The object type is SimplifiedObjectInfo.

prefix_list = None

The prefix list, type is list of str .

class oss2.models.ListPartsResult(resp)[source]

Bases: oss2.models.RequestResult

class oss2.models.LiveChannelAudioStat(codec='', sample_rate=0, bandwidth=0)[source]

Bases: object

Audio node in LiveStat

Parameters:
  • codec (str) – Audio codec.
  • sample_rate (int) – Sample rate
  • bandwidth (int) – Bandwidth
class oss2.models.LiveChannelHistory[source]

Bases: object

Pushing streaming record of the live channel.

class oss2.models.LiveChannelInfo(status='enabled', description='', last_modified=None, name=None, play_url=None, publish_url=None)[source]

Bases: object

Live channel configuration.

Parameters:
  • status (str) – status: The value is either “enabled” or “disabled”.
  • description (str) – The live channel’s description. The maximum length is 128 bytes.
  • target (LiveChannelInfoTarget) – The target informtion of a pushing streaming, including the parameters about the target protocol.
  • last_modified (Last modified time in unix time (int type), See Unix Time.) – The last modified time of the live channel. It is only used in ListLiveChannel.
  • name (str) – The live channel name.
  • play_url (str) – The play URL.
  • publish_url (str) – The publish URL.
class oss2.models.LiveChannelInfoTarget(type='HLS', frag_duration=5, frag_count=3, playlist_name='')[source]

Bases: object

Target information in the live channel,which includes the parameters of the target protocol.

Parameters:
  • type (str) – Prtocol, only HLS is supported for now.
  • frag_duration (int) – The expected time length in seconds of HLS protocol’s TS files.
  • frag_count (int) – TS file count in the m3u8 file of HLS protocol.
class oss2.models.LiveChannelList(prefix='', marker='', max_keys=100, is_truncated=False, next_marker='')[source]

Bases: object

The result of live channel list operation.

Parameters:
  • prefix (str) – The live channel to list
  • marker (str) – The paging marker in the live channel list operation.
  • max_keys (int) – Max entries to return.
  • is_truncated (bool) – Is there more live channels to list.
  • next_marker – The next paging marker
  • channels (list of LiveChannelInfo) – The live channel list returned.
class oss2.models.LiveChannelStat(status='', remote_addr='', connected_time='', video=None, audio=None)[source]

Bases: object

LiveStat result.

Parameters:
  • status – The live channel status
  • remote_addr (str) – Remote address
  • connected_time (int, unix time) – The connected time for the pusing streaming.
  • video (LiveChannelVideoStat) – Video description information
  • audio (LiveChannelAudioStat) – Audio description information
class oss2.models.LiveChannelVideoStat(width=0, height=0, frame_rate=0, codec='', bandwidth=0)[source]

Bases: object

The video node in LiveStat.

Parameters:
  • width (int) – Video width
  • height (int) – Video height
  • frame_rate (int) – Frame rate
  • codec (str) – codec
  • bandwidth (int) – Bandwidth of the video
class oss2.models.LiveRecord(start_time='', end_time='', remote_addr='')[source]

Bases: object

Pushing streaming record

Parameters:
  • start_time (int, see Unix Time.) – The start time of the push streaming.
  • end_time (int, see Unix Time for more information.) – The end time of the push streaming.
  • remote_addr (str) – The remote address of the pushing streaming.
class oss2.models.MultipartUploadInfo(key, upload_id, initiation_date)[source]

Bases: object

initiation_date = None

The initialization time of a multipart upload in Unix time whose type is int. See Unix Time for more information.

is_prefix()[source]

It returns True if it is a common prefix. Otherwise it returns False

key = None

File name

upload_id = None

Upload ID

class oss2.models.Owner(display_name, owner_id)[source]

Bases: object

class oss2.models.PartInfo(part_number, etag, size=None, last_modified=None)[source]

Bases: object

Part information.

This class is the output object of list_parts, and the input parameters for complete_multipart_upload.

Parameters:
  • part_number (int) – The part number (starting from 1)
  • etag (str) – ETag
  • size (int) – Part size in bytes. It is only used in list_parts result.
  • last_modified (int) – The time the part was last modified in Unix time. The type is int. See Unix Time for more information.
class oss2.models.PutObjectResult(resp)[source]

Bases: oss2.models.RequestResult

crc = None

CRC value of the file uploaded.

etag = None

HTTP ETag

class oss2.models.RequestResult(resp)[source]

Bases: object

headers = None

HTTP headers

request_id = None

Request ID which is used for tracking OSS requests. It is useful when submitting a customer ticket.

resp = None

HTTP response

status = None

HTTP status code (such as 200,404, etc)

class oss2.models.SimplifiedBucketInfo(name, location, creation_date)[source]

Bases: object

The single element type in the result of list_buckets .

creation_date = None

Bucket created time in Unix time. See Unix Time for more information.

location = None

Bucket location

name = None

Bucket name

class oss2.models.SimplifiedObjectInfo(key, last_modified, etag, type, size, storage_class)[source]

Bases: object

etag = None

HTTP ETag

is_prefix()[source]

It returns True if it is a common prefix(folder). Otherwise it returns False.

key = None

The file name or common prefix name (folder name).

last_modified = None

Last modified time.

size = None

File size

storage_class = None

Storage class (Standard, IA and Archive)

type = None

File type

class oss2.models.StorageTransition(days=None, created_before_date=None, storage_class=None)[source]

Bases: object

Transit objects

Parameters:
  • days – Transit objects whose last modified time is ‘days’ (days in the past).
  • created_before_date – Transit objects whose last modified time is earlier than created_before_date.
  • storage_class – The target storage type of object transited to OSS.

oss2.resumable module

oss2.resumable

The module contains the classes for resumable upload.

class oss2.resumable.ResumableDownloadStore(root=None, dir=None)[source]

Bases: oss2.resumable._ResumableStoreBase

The class for persisting downloading checkpoint information.

The checkpoint information is saved in a subfolder of root/dir/.

Parameters:
  • root (str) – Root folder, default is HOME.
  • dir (str) – Subfolder, default is _UPLOAD_TEMP_DIR
static make_store_key(bucket_name, key, filename)[source]
class oss2.resumable.ResumableStore(root=None, dir=None)[source]

Bases: oss2.resumable._ResumableStoreBase

The class for persisting uploading checkpoint information.

The checkpoint information would be a subfolder of root/dir/

Parameters:
  • root (str) – Root folder, default is HOME.
  • dir (str) – Subfolder, default is _UPLOAD_TEMP_DIR.
static make_store_key(bucket_name, key, filename)[source]
oss2.resumable.determine_part_size(total_size, preferred_size=None)[source]

Determine the part size of the multiparts upload.

Parameters:
  • total_size (int) – Total size to upload.
  • preferred_size (int) – User’s preferred size. By default it is defaults.part_size .
Returns:

Part size.

oss2.resumable.make_download_store(root=None, dir=None)[source]
oss2.resumable.make_upload_store(root=None, dir=None)[source]
oss2.resumable.resumable_download(bucket, key, filename, multiget_threshold=None, part_size=None, progress_callback=None, num_threads=None, store=None)[source]

Resumable download.

The implementation :
  1. Create a temporary file with the same original file name plus a random suffix.
  2. Download the OSS file with specified Range into the temporary file.
  3. Once the download is finished, rename the temp file with the target file name.

During a download, the checkpoint information (finished range) is stored in the disk as a checkpoint file.

If the download is interrupted, the download can resume from the checkpoint file if the source and target files match. Only the missing parts will be downloaded.

By default, the checkpoint file is in the Home subfolder. The subfolder for storing the checkpoint file can be specified through the store parameter.

Note

  1. For the same source and target file, at any given time, there should be only one running instance of this API. Otherwise multiple calls could lead to checkpoint files overwriting each other.
  2. Don’t use a small part size. The suggested size is no less than oss2.defaults.multiget_part_size.
  3. The API will overwrite the target file if it exists already.
  4. If CryptoBucket is used, the function will become a normal download.
Parameters:
  • bucketBucket instance.
  • key (str) – OSS key object.
  • filename (str) – Local file name.
  • multiget_threshold (int) – The threshold of the file size to use multiget download.
  • part_size (int) – The preferred part size. The actual part size might be slightly different according to determine_part_size().
  • progress_callback – Progress callback. See Upload or Download Progress for more information.
  • num_threads – Parallel thread number. The default value is oss2.defaults.multiget_num_threads.
  • store (ResumableDownloadStore) – Specifies the persistent storage for checkpoint information. For example, the folder of the checkpoint file.
Raises:

If the source OSS file does not exist NotFound is thrown . Other exceptions may be thrown due to other issues.

oss2.resumable.resumable_upload(bucket, key, filename, store=None, headers=None, multipart_threshold=None, part_size=None, progress_callback=None, num_threads=None)[source]

Uses multipart upload to upload a local file.

The oss2.defaults.multipart_num_threads is used as the default parallel thread count. The upload progress is saved to the local disk. If the upload is interrupted, and a new upload is started with the same local file and destination, the upload will resume from where the last upload stopped based on the checkpoint file. This function saves the upload progress to the HOME folder on the local disk by default.

Note

  1. If CryptoBucket is used, the function degrades to a normal upload operation.
Parameters:
  • bucketBucket instance.
  • key – The OSS object key.
  • filename – The name of the local file to be uploaded.
  • store – Upload progress information storage. ResumableStore is used if not specified. See ResumableStore for more information.
  • headers – HTTP headers for put_object or init_multipart_upload.
  • multipart_threshold – Files exceeding this size will be uploaded with multipart upload.
  • part_size – Part size. The value is calculated automatically if not specified.
  • progress_callback – The progress callback. See ref:progress_callback for more information.
  • num_threads – Upload parallel thread count. oss2.defaults.multipart_num_threads will be used if not specified.

oss2.task_queue module

class oss2.task_queue.TaskQueue(producer, consumers)[source]

Bases: object

get()[source]
ok()[source]
put(data)[source]
run()[source]

oss2.utils module

oss2.utils

Utils module

class oss2.utils.AESCipher(key=None, start=None)[source]

AES256 encryption implementation.

Parameters:
  • key (str) – Symmetric encrypted key.
  • start (str) – Symmetric encryption initial random value.

Note

Users can implement symmetric encryption algorithm of their own. 1: Provide a symmetric encryption algorithm name, ALGORITHM 2: Provide a static method to return the encryption key and the initial random value (if the algorithm does not require an initial random value, it also needs to be provided). 3: Provide encryption and decryption methods.

ALGORITHM = 'AES/GCM/NoPadding'
decrypt(enc)[source]
encrypt(raw)[source]
static get_key()[source]
static get_start()[source]
class oss2.utils.Crc64(init_crc=0)[source]

Bases: object

crc
update(data)[source]
class oss2.utils.SizedFileAdapter(file_object, size)[source]

Bases: object

This adapter guarantees reading of data up to only the specified size, even if the original file_object size is larger.

len
read(amt=None)[source]
oss2.utils.b64decode_from_string(data)[source]
oss2.utils.b64encode_as_string(data)[source]
oss2.utils.check_crc(operation, client_crc, oss_crc, request_id)[source]
oss2.utils.content_md5(data)[source]

Calculate the MD5 of the data. The return value is base64 encoded str.

The return value could be value of of HTTP Content-MD5 header.

oss2.utils.content_type_by_name(name)[source]

Return the Content-Type by file name.

oss2.utils.copyfileobj_and_verify(fsrc, fdst, expected_len, chunk_size=16384, request_id='')[source]

copy data from file-like object fsrc to file-like object fdst, and verify the length

oss2.utils.date_to_iso8601(d)[source]
oss2.utils.file_object_remaining_bytes(fileobj)[source]
oss2.utils.force_rename(src, dst)[source]
oss2.utils.how_many(m, n)[source]
oss2.utils.http_date(timeval=None)[source]

Return the HTTP standard GMT time string. If using strftime format, it would be ‘%a, %d %b %Y %H:%M:%S GMT’. But strftime() cannot be used as it is locale dependent.

oss2.utils.http_to_unixtime(time_string)[source]

Convert the HTTP date to Unix time (total seconds since 1970 Jan First, 00:00).

HTTP Date such as Sat, 05 Dec 2015 11:10:29 GMT

oss2.utils.is_ip_or_localhost(netloc)[source]

Determine whether the network address is IP or localhost.

oss2.utils.is_valid_bucket_name(name)[source]

Check if the bucket name is valid.

oss2.utils.iso8601_to_date(time_string)[source]
oss2.utils.iso8601_to_unixtime(time_string)[source]

Convert the ISO8601 time string (e.g. 2012-02-24T06:07:48.000Z) to Unix time in seconds

oss2.utils.make_cipher_adapter(data, cipher_callback)[source]

Return an adapter instance for encrypting during read.

Parameters:
  • data – It can be bytes, file object or iterable.
  • operation – Encrypt or decrypt operations.
  • key – The password in symmetric encryption which must be 16/24/32 bytes.
  • start – Counter initial value.
Returns:

Adapter that could call the encryption function.

oss2.utils.make_crc_adapter(data, init_crc=0)[source]

Return an adapter instance so that the CRC can be calculated during reading.

Parameters:
  • data – It can be bytes,file object or iterable.
  • init_crc – Init CRC value, optional.
Returns:

A adapter that can calls the CRC caluclating function.

oss2.utils.make_progress_adapter(data, progress_callback, size=None)[source]
Return an adapter instance so that the progress callback is called when reading the data.
When parameter size is not specified and cannot be dertermined. The total size in the callback is None.
Parameters:
  • data – It can be bytes,file object or iterable.
  • progress_callback – Progress callback. See Upload or Download Progress for more information.
  • size – Specify the data size, optional.
Returns:

The adapters that could call the progress callback.

oss2.utils.makedir_p(dirpath)[source]
oss2.utils.md5_string(data)[source]

Returns MD5 value of data in hex string (hexdigest()).

oss2.utils.random_aes256_key()[source]
oss2.utils.random_counter(begin=1, end=10)[source]
oss2.utils.set_content_type(headers, name)[source]

Set the Content-Type in headers by the name. If the content-type has been set, no-op and return.

oss2.utils.silently_remove(filename)[source]

Silently remove the file. If the file does not exist, no-op and return without error.

oss2.utils.to_unixtime(time_string, format_string)[source]

oss2.xml_utils module

oss2.xml_utils

Utility class for XML processing.

It includes two kind of APIs:
  • APIs starting with parse_: This is for paring the xml from OSS server
  • APIs starting with to_: This is for generating the xml to sent to OSS servers
oss2.xml_utils.parse_batch_delete_objects(result, body)[source]
oss2.xml_utils.parse_create_live_channel(result, body)[source]
oss2.xml_utils.parse_get_bucket_acl(result, body)[source]
oss2.xml_utils.parse_get_bucket_cors(result, body)[source]
oss2.xml_utils.parse_get_bucket_info(result, body)[source]
oss2.xml_utils.parse_get_bucket_lifecycle(result, body)[source]
oss2.xml_utils.parse_get_bucket_location(result, body)[source]
oss2.xml_utils.parse_get_bucket_logging(result, body)[source]
oss2.xml_utils.parse_get_bucket_referer(result, body)[source]
oss2.xml_utils.parse_get_bucket_stat(result, body)[source]
oss2.xml_utils.parse_get_bucket_websiste(result, body)[source]
oss2.xml_utils.parse_get_live_channel(result, body)[source]
oss2.xml_utils.parse_get_object_acl(result, body)
oss2.xml_utils.parse_init_multipart_upload(result, body)[source]
oss2.xml_utils.parse_lifecycle_abort_multipart_upload(abort_multipart_upload_node)[source]
oss2.xml_utils.parse_lifecycle_expiration(expiration_node)[source]
oss2.xml_utils.parse_lifecycle_storage_transitions(storage_transition_nodes)[source]
oss2.xml_utils.parse_list_buckets(result, body)[source]
oss2.xml_utils.parse_list_live_channel(result, body)[source]
oss2.xml_utils.parse_list_multipart_uploads(result, body)[source]
oss2.xml_utils.parse_list_objects(result, body)[source]
oss2.xml_utils.parse_list_parts(result, body)[source]
oss2.xml_utils.parse_live_channel_history(result, body)[source]
oss2.xml_utils.parse_live_channel_stat(result, body)[source]
oss2.xml_utils.parse_stat_audio(audio_node, audio)[source]
oss2.xml_utils.parse_stat_video(video_node, video)[source]
oss2.xml_utils.to_batch_delete_objects_request(keys, quiet)[source]
oss2.xml_utils.to_complete_upload_request(parts)[source]
oss2.xml_utils.to_create_live_channel(live_channel)[source]
oss2.xml_utils.to_put_bucket_config(bucket_config)[source]
oss2.xml_utils.to_put_bucket_cors(bucket_cors)[source]
oss2.xml_utils.to_put_bucket_lifecycle(bucket_lifecycle)[source]
oss2.xml_utils.to_put_bucket_logging(bucket_logging)[source]
oss2.xml_utils.to_put_bucket_referer(bucket_referer)[source]
oss2.xml_utils.to_put_bucket_website(bucket_websiste)[source]

Module contents