You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何在Amazon S3中用Multipart传图及无凭证仅用Presigned URL传文件?

Hey there, let's break down your two Amazon S3 questions clearly, with practical steps and examples:

1. 如何在Amazon S3存储服务中使用Multipart(分块)方式上传图片?

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 an Upload 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.jpg
    

    This returns a JSON response with the UploadId and bucket/key details—save this UploadId, 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 the UploadPart API, specifying the UploadId, 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_ID
    

    Each upload returns an ETag value—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 the CompleteMultipartUpload API, referencing your UploadId and 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-upload to 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.
2. 若仅持有Presigned URL(预签名URL),无访问凭证时能否在Amazon S3中上传文件?

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 ツ

火山引擎 最新活动