将对象上传到目录存储桶 - Amazon Simple Storage Service

将对象上传到目录存储桶

创建 Amazon S3 目录桶后,可以将对象上传到该桶。以下示例显示如何使用 S3 控制台和 AWS SDK 将对象上传到目录桶。有关使用 S3 Express One Zone 进行批量上传对象操作的信息,请参阅对象管理

  1. 登录到 AWS Management Console,然后通过以下网址打开 Amazon S3 控制台:https://console.aws.amazon.com/s3/

  2. 在左侧导航窗格中,选择目录存储桶

  3. 选择要将文件夹或文件上传到的桶的名称。

  4. 对象列表中,选择上传

  5. 上传页面上,执行以下操作之一:

    • 将文件和文件夹拖放到虚线上传区域。

    • 选择添加文件添加文件夹,选择要上传的文件或文件夹,然后选择打开上传

  6. 校验和下,选择要使用的校验和函数

    (可选)如果您要上传大小不到 16MB 的单个对象,也可以指定预先计算的校验和值。当您提供预先计算的值时,Amazon S3 会将该值与它使用所选校验和函数计算的值进行比较。如果值不匹配,则上传不会开始。

  7. 权限属性部分中的选项会自动设置为默认设置,无法修改。将自动启用“屏蔽公共访问权限”,而无法为目录桶启用 S3 版本控制和 S3 对象锁定。

    (可选)如果要以键值对的形式向对象添加元数据,请展开属性部分,然后在元数据部分中选择添加元数据

  8. 要上传列出的文件和文件夹,请选择上传

    Amazon S3 会上传您的对象和文件夹。上传完成后,您可以在上传:状态页面上看到成功消息。

SDK for Java 2.x
public static void putObject(S3Client s3Client, String bucketName, String objectKey, Path filePath) { //Using File Path to avoid loading the whole file into memory try { PutObjectRequest putObj = PutObjectRequest.builder() .bucket(bucketName) .key(objectKey) //.metadata(metadata) .build(); s3Client.putObject(putObj, filePath); System.out.println("Successfully placed " + objectKey +" into bucket "+bucketName); } catch (S3Exception e) { System.err.println(e.getMessage()); System.exit(1); } }
SDK for Python
import boto3 import botocore from botocore.exceptions import ClientError def put_object(s3_client, bucket_name, key_name, object_bytes): """ Upload data to a directory bucket. :param s3_client: The boto3 S3 client :param bucket_name: The bucket that will contain the object :param key_name: The key of the object to be uploaded :param object_bytes: The data to upload """ try: response = s3_client.put_object(Bucket=bucket_name, Key=key_name, Body=object_bytes) print(f"Upload object '{key_name}' to bucket '{bucket_name}'.") return response except ClientError: print(f"Couldn't upload object '{key_name}' to bucket '{bucket_name}'.") raise def main(): # Share the client session with functions and objects to benefit from S3 Express One Zone auth key s3_client = boto3.client('s3') # Directory bucket name must end with --zone-id--x-s3 resp = put_object(s3_client, 'doc-bucket-example--use1-az5--x-s3', 'sample.txt', b'Hello, World!') print(resp) if __name__ == "__main__": main()

以下 put-object 示例命令显示如何使用 AWS CLI 从 Amazon S3 上传对象。要运行此命令,请将 user input placeholders 替换为您自己的信息。

aws s3api put-object --bucket bucket-base-name--zone-id--x-s3 --key sampleinut/file001.bin --body bucket-seed/file001.bin

有关更多信息,请参阅 AWS CLI 命令参考 中的 put-object