文件下载

用户可以通过以下方式从NOS获取文件:

  • 下载文件

下载文件

以下源代码实现下载文件到内存中::

import boto3
import botocore

access_key = "您的accessKeyId"
secret_key = "您的accessKeySecret"
end_point = "建桶时选择的的区域域名"  #需要加入scheme(http || https)
bucket = "使用的桶名,注意命名规则"
object = "使用的对象名,注意命名规则"

s3Client = boto3.client(
    's3',
    aws_access_key_id=access_key,
    aws_secret_access_keysecret_key,
    endpoint_url=end_point,
)

try:
    result = s3Client.get_object(Bucket=bucketName, Key=key)
    fp = result.get("Body")
    object_str = fp.read()
    print("object content: %s", object_str)
except botocore.exceptions.ClientError as e:
    print(
        "ServiceException: %s\n"
        "status_code: %s\n"
        "error_code: %s\n"
        "request_id: %s\n"
        "message: %s\n"
    % (
        e,
        e.response['ResponseMetadata']['HTTPStatusCode'],  # 错误http状态码
        e.response["Error"]['Code'],  # NOS服务器定义错误类型
        e.response['ResponseMetadata']['HTTPHeaders']['x-nos-request-id'],  # NOS服务器定义错误码
        e.response['Error']['Message'],  # 请求ID,有利于nos开发人员跟踪异常请求的错误原因
    ))

Attention

1.下载内容也可以存储到文件中