如何在Amazon S3中用Multipart传图及无凭证仅用Presigned URL传文件?
Hey there, let's break down your two Amazon S3 questions clearly, with practical steps and examples:
Multipart upload is perfect for larger files (Amazon recommends it for files over 100MB, though you can use it for any file over 5MB). It lets you split your image into smaller chunks, upload them in parallel, and then combine them into a single object in S3. Here's a step-by-step guide:
核心操作步骤
Step 1: 初始化分块上传
First, you need to start the upload session and get anUpload ID—this is your unique identifier for the entire multipart process. Using the AWS CLI, the command looks like this:aws s3api create-multipart-upload --bucket your-bucket-name --key path/to/your-image.jpgThis returns a JSON response with the
UploadIdand bucket/key details—save thisUploadId, you'll need it for every subsequent step.Step 2: 上传单个分块
Split your image into chunks (each part must be at least 5MB, except the final part which can be smaller). For each chunk, upload it using theUploadPartAPI, specifying theUploadId,PartNumber(a sequential number from 1 to 10000), and the file chunk.Example CLI command for uploading part 1:
aws s3api upload-part --bucket your-bucket-name --key path/to/your-image.jpg --part-number 1 --body chunk1.jpg --upload-id YOUR_UPLOAD_IDEach upload returns an
ETagvalue—save all PartNumber and ETag pairs in a JSON file (e.g.,parts.json) like this:{ "Parts": [ { "ETag": "\"abc123...\"", "PartNumber": 1 }, { "ETag": "\"def456...\"", "PartNumber": 2 } ] }Step 3: 完成分块上传
Once all parts are uploaded successfully, tell S3 to combine them into a single object using theCompleteMultipartUploadAPI, referencing yourUploadIdand the parts JSON file:aws s3api complete-multipart-upload --bucket your-bucket-name --key path/to/your-image.jpg --upload-id YOUR_UPLOAD_ID --multipart-upload file://parts.json
重要注意事项
- If you need to cancel the upload (e.g., due to a failure), use
aws s3api abort-multipart-uploadto clean up incomplete parts—S3 charges for storage of uncompleted parts after a certain period. - You can upload parts in parallel to speed up the process, which is a huge advantage for large high-resolution images.
- The maximum number of parts per upload is 10000.
Absolutely—this is one of the core use cases for Presigned URLs!
A Presigned URL is generated by an AWS user who has the necessary permissions (like s3:PutObject for uploads). When creating the URL, the user specifies the allowed action (e.g., PUT), the target bucket/object, and an expiration time. Once generated, anyone with the URL can perform the specified action without needing their own AWS access keys.
如何用Presigned URL上传文件
For example, if you have a Presigned PUT URL, you can upload a file using a simple tool like curl:
curl -X PUT -T "your-local-image.jpg" "https://your-bucket.s3.amazonaws.com/your-image.jpg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=..."
Or if you're working with a browser, you can build a simple HTML form that submits a PUT request directly to the URL (some configurations may require matching the content-type specified when generating the URL).
关键要点
- The URL must be generated for the PUT operation—a GET-only Presigned URL won't work for uploads.
- The URL will expire after the time window set when it was created (you can configure this from 1 minute to 7 days).
- Your upload must match the parameters defined when generating the URL (e.g., you can't change the object key, and if a content-type was specified, your upload must use that same type).
内容的提问来源于stack exchange,提问作者Vishal Patoliya ツ




