**文件管理** ############################## 在NOS中,用户可以通过一系列的接口管理桶(Bucket)中的文件(Object),比如list_objects,delete_object,copy_object等。 列出桶中的文件 ============================= 您可以使用list_objects列出桶中的文件:: import boto3 import botocore access_key = "您的accessKeyId" secret_key = "您的accessKeySecret" end_point = "建桶时选择的的区域域名" s3Client = boto3.client('s3',aws_access_key_id=access_key,aws_secret_access_key=secret_key,endpoint_url=end_point,) try: object_lists = s3Client.list_objects(Bucket=bucketName) for ob in object_lists['Contents']: print("key : {0}".format(ob["Key"])) except botocore.exceptions.ClientError as e: print( "ServiceError: %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开发人员跟踪异常请求的错误原因 )) except botocore.exceptions.ParamValidationError as e: print( "ClientError: %s\n" "message: %s\n" % ( e, e.fmt )) list_objects可以指定的可选参数如下所示: +------------------------+---------------------------------------------------------------------------------------------------------+ |参数 | 说明 | +========================+=========================================================================================================+ | delimiter |用于对Object名字进行分组的字符。所有名字包含指定的前缀且第一次出现delimiter字符之间的object作为一组元素 | +------------------------+---------------------------------------------------------------------------------------------------------+ | prefix |限定返回的object key必须以prefix作为前缀。注意使用prefix查询时,返回的key中仍会包含prefix | +------------------------+---------------------------------------------------------------------------------------------------------+ | limit |限定此次返回object的最大数,如果不设定,默认为100 | +------------------------+---------------------------------------------------------------------------------------------------------+ | marker |设定结果从marker之后按字母排序的第一个开始返回 | +------------------------+---------------------------------------------------------------------------------------------------------+ 删除单个文件 =========================== 您可以使用delete_object删除单个需要删除的文件:: import boto3 import botocore try: s3Client.delete_object(bucket, object) except botocore.exceptions.ClientError as e: print( "ServiceError: %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开发人员跟踪异常请求的错误原因 )) except botocore.exceptions.ParamValidationError as e: print( "ClientError: %s\n" "message: %s\n" % ( e, e.fmt )) 删除多个文件 ============================ 您可以使用delete_objects批量删除文件:: import boto3 import botocore keys = { 'Objects': [ { 'Key': 'string', }, { 'Key': 'aa' }, { 'Key': key } ], 'Quiet': True } #如果Quiet=True,那么正确删除的将不显示,反之会显示 try: client.delete_objects(bucket, keys) except botocore.exceptions.ClientError as e: print( "ServiceError: %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开发人员跟踪异常请求的错误原因 )) except botocore.exceptions.ParamValidationError as e: print( "ClientError: %s\n" "message: %s\n" % ( e, e.fmt )) 拷贝文件 =========================== 您可以使用copy_object拷贝文件:: import boto3 import botocore try: copySource = "/{0}/{1}".format(bucketName, key) #格式按照这个来,否者NOS不接受该请求 response = s3Client.copy_object(CopySource=copySource, Bucket=destBucketName, Key=destKey) except botocore.exceptions.ClientError as e: print( "ServiceError: %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开发人员跟踪异常请求的错误原因 )) except botocore.exceptions.ParamValidationError as e: print( "ClientError: %s\n" "message: %s\n" % ( e, e.fmt )) .. attention:: 1. 支持跨桶的文件copy 生成私有对象可下载的URL链接 =========================== 您可以使用generate_presigned_url来生成私有对象的可下载的URL:: url = s3Client.generate_presigned_url( ClientMethod='get_object', Params={ 'Bucket': bucketName, 'Key': objectName }, ExpiresIn = 10 # 单位s,默认3600s ) print(url) # print的内容是可以直接用来下载对象的,可以直接贴到浏览器中下载