简体   繁体   English

如何在图像上传 PUT 请求期间设置/更新 AWS s3 object 元数据到签名的 url?

[英]How do I set/update AWS s3 object metadata during image upload PUT request to signed url?

I'm trying to include the name of the file that is uploaded to AWS S3 and given a random/unique name in a NextJS app.我正在尝试包含上传到 AWS S3 并在 NextJS 应用程序中赋予随机/唯一名称的文件的名称。

I can set metadata from the backend, but I would like to update it from my put request (where the image is actually uploaded) to the signed URL.我可以从后端设置元数据,但我想将其从我的 put 请求(实际上传图像的位置)更新为签名的 URL。 How would I do this?我该怎么做?

To be clear: I would like to set metadata when I do a PUT request to the signed URL.需要明确的是:当我向签名的 URL 发出 PUT 请求时,我想设置元数据。 I currently have it set to "none" on the backend to avoid forbidden errors (and this shows up as metadata in s3).我目前在后端将其设置为“无”以避免被禁止的错误(这在 s3 中显示为元数据)。 Is this possible to update that metadata from my PUT request is there another approach I should take?这是否可以从我的 PUT 请求中更新元数据我应该采取另一种方法吗? Thanks!谢谢!

// Backend code to get signed URL

async function handler(req, res) {
  if (req.method === 'GET') {
    const key = `content/${uuidv4()}.jpeg`;

    s3.getSignedUrl(
      'putObject',
      {
        Bucket: 'assets',
        ContentType: 'image/jpeg',
        Key: key,
        Expires: 5 * 60, 
        Metadata: {
          'file-name': "none",
        }
      },
      (err, url) => res.send({ key, url }) 
    );
  }
// Frontend code 

const [file, setFile] = useState(null);

const onFileUpload = async (e) => {
    e.preventDefault();

    const uploadConfig = await fetch('/api/upload');
    const uploadURL = await uploadConfig.json();

    await fetch(uploadURL.url, {
      body: file,
      method: 'PUT',
      headers: {
        'Content-Type': file.type,
        'x-amz-meta-file-name': 'updated test name',
      },
    });
  };

It isn't possible to do that with presigned urls.使用预签名的 url 是不可能的。 When you create the presigned url, all properties are pre-filled.当您创建预签名 url 时,所有属性都已预填充。 You can't change them, when uploading the file.上传文件时,您无法更改它们。 The only thing you can control is the object's data.您唯一可以控制的是对象的数据。 The bucket, the key and the metadata (and all other parameters of put_object) are predefined.存储桶、密钥和元数据(以及 put_object 的所有其他参数)是预定义的。 This is also the case for generate_presigned_post. generate_presigned_post 也是如此。 All fields are prefilled.所有字段均已预先填写。

This makes sense, as the back-end grants the permissions and needs to decide on these.这是有道理的,因为后端授予权限并需要决定这些权限。 Also the implementation will be much more complicated, as presigned urls support all client methods, which have different parameters.此外,实现会更加复杂,因为预签名的 url 支持所有具有不同参数的客户端方法。

The only way you could do it, is to generate urls on-demand.您可以做到这一点的唯一方法是按需生成 url。 First generate the pre-signed url, based on the name selected by the client and then do the upload.首先根据客户端选择的名称生成预签名的url,然后进行上传。 You will need two round-trips for every file.每个文件都需要两次往返。 One to your server, for generating the url and one to S3 for the uploading.一个到您的服务器,用于生成 url,一个到 S3 用于上传。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS S3 使用预签名的 URL 更新映像(Axios-PUT 请求) - AWS S3 update image using pre-signed URL (Axios-PUT Request) 在s3 aws中为对象创建签名的url时如何设置内容类型? - how do I set content-type when creating a signed url for an object in s3 aws? 使用 lamda 上传 S3 图像的签名 URL 上的“PUT”请求上出现 403 - 403 on `PUT` request on signed url for S3 image upload using lamda 通过预签名URL将PUT上传到S3时拒绝签名 - Signature Denied in PUT upload to S3 with Pre-Signed URL 使用预先签名的POST URL将文件上载到AWS S3时设置随机文件名 - Set random file name when upload file to AWS S3 with pre-signed POST url 如何将文件上传到AWS中的预签名URL? - How do I upload a file to a pre-signed URL in AWS? 如何通过 Node.js 中的预签名 URL 将文件上传到 AWS S3 存储桶 - How to Upload File to AWS S3 Bucket via pre signed URL in Node.js 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? S3使用预先签名的URL从浏览器上传图片 - S3 Upload image with pre-signed url from browser 如何使用预先签名的 url 将对象放入 amazon s3? - How do I put object to amazon s3 using presigned url?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM