Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions alibabacloud_oss_v2/encryption_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from .crypto import MasterCipher, Envelope, ContentCipherBuilder, ContentCipher, CipherData
from .crypto.aes_ctr_cipher import AESCtrCipherBuilder
from .crypto.aes_ctr import _BLOCK_SIZE_LEN
from .filelike import ReadOnlyFile

class EncryptionMultiPartContext:
"""EncryptionMultiPartContext save encryption or decryption information
Expand Down Expand Up @@ -180,6 +181,30 @@ def list_parts(self, request: models.ListPartsRequest, **kwargs

return self._client.list_parts(request, **kwargs)

def open_file(self, bucket: str, key: str,
version_id: Optional[str] = None,
request_payer: Optional[str] = None,
**kwargs) -> ReadOnlyFile:
"""OpenFile opens the named file for reading.

Args:
bucket (str, required): The name of the bucket.
key (str, required): The name of the object.
version_id (str, optional): The version ID of the object.
request_payer (str, optional): To indicate that the requester is aware that the request and data download will incur costs

Returns:
ReadOnlyFile: _description_
"""
return ReadOnlyFile(
self,
bucket=bucket,
key=key,
version_id=version_id,
request_payer=request_payer,
**kwargs
)

def _get_ccbuilder(self, envelope: Envelope ) -> ContentCipherBuilder:
return self._ccbuilders.get(envelope.mat_desc or '', self._defualt_ccbuilder)

Expand Down
53 changes: 53 additions & 0 deletions sample/eclient_file_like_read_only.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import argparse
import alibabacloud_oss_v2 as oss


parser = argparse.ArgumentParser(description="file like read only file sample")
parser.add_argument('--region', help='The region in which the bucket is located.', required=True)
parser.add_argument('--bucket', help='The name of the bucket.', required=True)
parser.add_argument('--endpoint', help='The domain names that other services can use to access OSS')
parser.add_argument('--key', help='The name of the object.', required=True)

RSA_PUBLIC_KEY = """-----BEGIN PUBLIC KEY-----
MIGfMA0G6mse2QsIgz3******GBcom6kEF6MmR1EKixaQIDAQAB
-----END PUBLIC KEY-----"""

RSA_PRIVATE_KEY = """-----BEGIN PRIVATE KEY-----
MIICdQIBADANBgk******ItewfwXIL1Mqz53lO/gK+q6TR92gGc+4ajL
-----END PRIVATE KEY-----"""


def main():

args = parser.parse_args()

# Loading credentials values from the environment variables
credentials_provider = oss.credentials.EnvironmentVariableCredentialsProvider()

# Using the SDK's default configuration
cfg = oss.config.load_default()
cfg.credentials_provider = credentials_provider
cfg.region = args.region


if args.endpoint is not None:
cfg.endpoint = args.endpoint

client = oss.Client(cfg)

mc = oss.crypto.MasterRsaCipher(
mat_desc={"desc": "your master encrypt key material describe information"},
public_key=RSA_PUBLIC_KEY,
private_key=RSA_PRIVATE_KEY
)
encryption_client = oss.EncryptionClient(client, mc)

rf: oss.ReadOnlyFile = None
with encryption_client.open_file(args.bucket, args.key) as f:
rf = f
print(rf.read().decode())


if __name__ == "__main__":
main()

Loading