airfs.storage.oss

Alibaba Cloud OSS

New in version 1.0.0.

Mount

OSS can be mounted using the airfs mount function.

storage_parameters await arguments to pass to oss2.Auth, oss2.AnonymousAuth or oss2.StsAuth classes from oss2 Python library (The class selection is done automatically based on parameters found in storage_parameters).

airfs also requires one extra argument, the endpoint, which is basically the URL of the OSS Alibaba region to use. (See endpoint argument of the oss2.Bucket class)

This example shows the mount of OSS with the minimal configuration:

import airfs

# Mount OSS manually (Minimal configuration)
airfs.mount(
    storage='oss',
    storage_parameters=dict(
        access_key_id='my_access_key_id',
        access_key_secret='my_access_key_secret',
        endpoint='http://oss-my_region.aliyuncs.com
    )
)

# Call of airfs on an OSS object.
with airfs.open('oss://my_bucket/my_object', 'rt') as file:
    text = file.read()

Limitation

Only one OSS configuration can be mounted simultaneously.

Files objects classes

Alibaba cloud OSS.

class airfs.storage.oss.OSSBufferedIO(*args, **kwargs)[source]

Buffered binary OSS Object I/O.

MINIMUM_BUFFER_SIZE = 102400

Minimal buffer_size in bytes (OSS multipart upload minimal part size)

close()

Flush the write buffers of the stream if applicable and close the object.

detach()

Disconnect this buffer from its underlying raw stream and return it.

After the raw stream has been detached, the buffer is in an unusable state.

fileno()

Returns underlying file descriptor if one exists.

OSError is raised if the IO object does not use a file descriptor.

flush()

Flush the write buffers of the stream if applicable.

isatty()

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

property mode

The mode.

Returns:

Mode.

Return type:

str

property name

The file name.

Returns:

Name.

Return type:

str

peek(size=-1)

Return bytes from the stream without advancing the position.

Parameters:

size (int) – Number of bytes to read. -1 to read the full stream.

Returns:

bytes read

Return type:

bytes

property raw

The underlying raw stream.

Returns:

Raw stream.

Return type:

ObjectRawIOBase subclass

read(size=-1)

Read the object content.

Read and return up to size bytes, with at most one call to the underlying raw stream.

Use at most one call to the underlying raw stream’s read method.

Parameters:

size (int) – Number of bytes to read. -1 to read the stream until the end.

Returns:

Object content

Return type:

bytes

read1(size=-1)

Read the object content.

Read and return up to size bytes, with at most one call to the underlying raw stream.

Use at most one call to the underlying raw stream’s read method.

Parameters:

size (int) – Number of bytes to read. -1 to read the stream until the end.

Returns:

Object content

Return type:

bytes

readable()

Return True if the stream can be read from.

If False, read() will raise OSError.

Returns:

Supports reading.

Return type:

bool

readinto(b)

Read the object content into a buffer.

Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read.

Parameters:

b (bytes-like object) – buffer.

Returns:

number of bytes read

Return type:

int

readinto1(b)

Read the object content into a buffer.

Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read.

Use at most one call to the underlying raw stream’s readinto method.

Parameters:

b (bytes-like object) – buffer.

Returns:

number of bytes read

Return type:

int

readline(size=-1, /)

Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

readlines(hint=-1, /)

Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

seek(offset, whence=0)

Change the stream position to the given byte offset.

Parameters:
  • offset – Offset is interpreted relative to the position indicated by whence.

  • whence – The default value for whence is SEEK_SET. Values are: SEEK_SET or 0 – Start of the stream (the default); offset should be zero or positive SEEK_CUR or 1 – Current stream position; offset may be negative SEEK_END or 2 – End of the stream; offset is usually negative

Returns:

The new absolute position.

Return type:

int

seekable()

Return True if the stream supports random access.

If False, seek(), tell() and truncate() will raise OSError.

Returns:

Supports random access.

Return type:

bool

tell()

Return the current stream position.

Returns:

Stream position.

Return type:

int

truncate()

Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.

writable()

Return True if the stream supports writing.

If False, write() and truncate() will raise OSError.

Returns:

Supports writing.

Return type:

bool

write(b)

Write into the object.

Write the given bytes-like object, b, to the underlying raw stream, and return the number of bytes written.

Parameters:

b (bytes-like object) – Bytes to write.

Returns:

The number of bytes written.

Return type:

int

writelines(lines, /)

Write a list of lines to stream.

Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.

class airfs.storage.oss.OSSRawIO(name, mode='r', storage_parameters=None, **kwargs)[source]

Binary OSS Object I/O.

Parameters:
  • name (path-like object) – URL or path to the file which will be opened.

  • mode (str) – The mode can be ‘r’, ‘w’, ‘a’ for reading (default), writing or appending.

  • storage_parameters (dict) – OSS2 Auth keyword arguments and endpoint. This is generally OSS credentials and configuration.

  • unsecure (bool) – If True, disables TLS/SSL to improve transfer performance. But makes connection unsecure.

close()

Flush the write buffers of the stream if applicable and close the object.

fileno()

Returns underlying file descriptor if one exists.

OSError is raised if the IO object does not use a file descriptor.

flush()

Flush.

Flush the write buffers of the stream if applicable and save the object on the storage.

isatty()

Return whether this is an ‘interactive’ stream.

Return False if it can’t be determined.

property mode

The mode.

Returns:

Mode.

Return type:

str

property name

The file name.

Returns:

Name.

Return type:

str

readable()

Return True if the stream can be read from.

If False, read() will raise OSError.

Returns:

Supports reading.

Return type:

bool

readall()

Read and return all the bytes from the stream until EOF.

Returns:

Object content

Return type:

bytes

readinto(b)

Read the object content into a buffer.

Read bytes into a pre-allocated, writable bytes-like object b, and return the number of bytes read.

Parameters:

b (bytes-like object) – buffer.

Returns:

number of bytes read

Return type:

int

readline(size=-1, /)

Read and return a line from the stream.

If size is specified, at most size bytes will be read.

The line terminator is always b’n’ for binary files; for text files, the newlines argument to open can be used to select the line terminator(s) recognized.

readlines(hint=-1, /)

Return a list of lines from the stream.

hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.

seek(offset, whence=0)

Change the stream position to the given byte offset.

Parameters:
  • offset (int) – Offset is interpreted relative to the position indicated by whence.

  • whence (int) – The default value for whence is SEEK_SET. Values are: SEEK_SET or 0 – Start of the stream (the default); offset should be zero or positive SEEK_CUR or 1 – Current stream position; offset may be negative SEEK_END or 2 – End of the stream; offset is usually negative

Returns:

The new absolute position.

Return type:

int

seekable()

Return True if the stream supports random access.

If False, seek(), tell() and truncate() will raise OSError.

Returns:

Supports random access.

Return type:

bool

tell()

Return the current stream position.

Returns:

Stream position.

Return type:

int

truncate()

Truncate file to size bytes.

File pointer is left unchanged. Size defaults to the current IO position as reported by tell(). Returns the new size.

writable()

Return True if the stream supports writing.

If False, write() and truncate() will raise OSError.

Returns:

Supports writing.

Return type:

bool

write(b)

Write into the object.

Write the given bytes-like object, b, to the underlying raw stream, and return the number of bytes written.

Parameters:

b (bytes-like object) – Bytes to write.

Returns:

The number of bytes written.

Return type:

int

writelines(lines, /)

Write a list of lines to stream.

Line separators are not added, so it is usual for each of the lines provided to have a line separator at the end.