AWS 文档 AWS SDK示例 GitHub 存储库中还有更多SDK示例。
本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
与 AWS SDK或CreateMultipartUpload
一起使用 CLI
以下代码示例演示如何使用 CreateMultipartUpload
。
操作示例是大型程序的代码摘录,必须在上下文中运行。您可以在以下代码示例中查看此操作的上下文:
- C++
-
- SDK对于 C++
-
//! Create a multipart upload.
/*!
\param bucket: The name of the S3 bucket where the object will be uploaded.
\param key: The unique identifier (key) for the object within the S3 bucket.
\param client: The S3 client instance used to perform the upload operation.
\return Aws::String: Upload ID or empty string if failed.
*/
Aws::String
AwsDoc::S3::createMultipartUpload(const Aws::String &bucket, const Aws::String &key,
Aws::S3::Model::ChecksumAlgorithm checksumAlgorithm,
const Aws::S3::S3Client &client) {
Aws::S3::Model::CreateMultipartUploadRequest request;
request.SetBucket(bucket);
request.SetKey(key);
if (checksumAlgorithm != Aws::S3::Model::ChecksumAlgorithm::NOT_SET) {
request.SetChecksumAlgorithm(checksumAlgorithm);
}
Aws::S3::Model::CreateMultipartUploadOutcome outcome =
client.CreateMultipartUpload(request);
Aws::String uploadID;
if (outcome.IsSuccess()) {
uploadID = outcome.GetResult().GetUploadId();
} else {
std::cerr << "Error creating multipart upload: " << outcome.GetError().GetMessage() << std::endl;
}
return uploadID;
}
- CLI
-
- AWS CLI
-
以下命令在存储桶 my-bucket
中创建带有密钥 multipart/01
的分段上传:
aws s3api create-multipart-upload --bucket my-bucket
--key 'multipart/01
'
输出:
{
"Bucket": "my-bucket",
"UploadId": "dfRtDYU0WWCCcH43C3WFbkRONycyCpTJJvxu2i5GYkZljF.Yxwh6XG7WfS2vC4to6HiV6Yjlx.cph0gtNBtJ8P3URCSbB7rjxI5iEwVDmgaXZOGgkk5nVTW16HOQ5l0R",
"Key": "multipart/01"
}
完成的文件在存储桶 my-bucket
中名为 multipart
文件夹中将命名为 01
。保存上传 ID、密钥和存储桶名称以供 upload-part
命令使用。
- Rust
-
- SDK对于 Rust
-
let multipart_upload_res: CreateMultipartUploadOutput = client
.create_multipart_upload()
.bucket(&bucket_name)
.key(&key)
.send()
.await
.unwrap();