3. 快速入门¶
请确认您已经熟悉NOS的基本概念,如Bucket、Object、EndPoint、AccessKeyId和AccessKeySecret等。 本节您将看到如何快速的使用AWS PYTHON SDK,完成常用的操作,上传文件、下载文件等。
3.1. 常用类¶
常用类 | 备注 |
---|---|
boto3.client | AWS客户端类,用户通过Client调用服务 |
botocore.exceptions.ClientError | NOS服务器返回的异常 |
3.2. 基本操作¶
3.2.1. 上传文件¶
对象(Object)是NOS中最基本的数据单元,您可以把它简单的理解为文件,以下代码可以实现简单的对象上传:
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:
response = s3Client.put_object(Bucket=bucketName, Key=key, Body="hello")
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
对象命名规则请参见 API 手册 对象
更多的上传文件信息,请参见 AWS-PYTHON-SDK 上传文件
3.2.2. 下载文件¶
上传对象成功之后,您可以读取它的内容,以下代码可以实现文件的下载:
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:
response = s3Client.get_object(Bucket=bucketName, Key=key)
fp = response.get("Body")
object_str = fp.read()
print("object content: {0}".format(object_str))
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
))
更多的下载文件信息,请参见 AWS-PYTHON-SDK 下载文件
3.2.3. 列举文件¶
当上传文件成功之后,可以查看桶中包含的文件列表,以下代码展示如何列举桶内的文件:
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
))
更多的管理文件信息,请参见 AWS-PYTHON-SDK 文件管理
3.2.4. 删除文件¶
文件上传成功后,可以指定删除桶中的文件,以下代码实现桶中文件的删除:
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
))