This topic describes how to use the File-Like method provided by Object Storage Service (OSS) SDK for Python 2.0 to access objects in a bucket.
Usage notes
The sample code in this topic uses the China (Hangzhou) region (
cn-hangzhou) as an example. The public endpoint is used by default. If you access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For more information about OSS regions and their corresponding endpoints, see OSS regions and endpoints.In this topic, access credentials obtained from environment variables are used. For more information about how to configure access credentials, see Configure access credentials.
To download a file, you must have the
oss:GetObjectpermission. For more information, see Grant custom permissions to a RAM user.
Method
The File-Like method provided by OSS SDK for Python 2.0 lets you access objects in a bucket using the ReadOnlyFile method.
The ReadOnlyFile method provides the single stream and prefetch modes. You can change the number of tasks in parallel to improve the read speed.
The interface includes a built-in reconnection mechanism to handle connection drops, which improves robustness in complex network environments.
class ReadOnlyFile:
...
def open_file(self, bucket: str, key: str, version_id: Optional[str] = None, request_payer: Optional[str] = None, **kwargs) -> ReadOnlyFile:
...Request parameters
Parameter | Type | Description |
bucket | str | The name of the bucket. |
key | str | The name of the object. |
version_id | str | The version number of the specified object. This parameter is valid only if multiple versions of the object exist. |
request_payer | str | Specifies that if pay-by-requester is enabled, RequestPayer must be set to requester. |
**kwargs | Any | Optional. The options that you want to configure, which are of the Dictionary type. |
Options of kwargs
Option | Type | Description |
enable_prefetch | bool | Specifies whether to enable the prefetch mode. By default, the prefetch mode is disabled. |
prefetch_num | int | The number of prefetched chunks. Default value: 3. This option is valid when the prefetch mode is enabled. |
chunk_size | int | The size of each prefetched chunk. Default value: 6. Unit: MiB. This option is valid when the prefetch mode is enabled. |
prefetch_threshold | int | The number of bytes to be read in sequence before the prefetch mode is enabled. Default value: 20. Unit: MiB. This option is valid when the prefetch mode is enabled. |
block_size | int | The size of a chunk. Default value: None. |
Response parameters
Parameter | Type | Description |
file | ReadOnlyFile | The ReadOnlyFile instance. |
Common methods of ReadOnlyFile
Method | Description |
close(self) | Closes the file handles to release resources, such as memory and active sockets. |
read(self, n=None) | Reads a byte whose length is len(p) from the data source, stores the byte in p, and returns the number of bytes that are read and the encountered errors. |
seek(self, pos, whence=0) | Specifies the offset for the next read or write. Valid values of whence: 0: the head. 1: the current offset. 2: the tail. |
Stat() (os.FileInfo, error) | Queries object information, including the object size, last modified time, and metadata. |
Note: If the prefetch mode is enabled and multiple out-of-order reads occur, the single stream mode is automatically used.
Examples
References
For more information about File-Like, visit File-Like.