NodeJS 仅当在www.example上部署应用程序时,尝试将图像上传到AWS S3 Bucket时超时 www.example.com

w41d8nur  于 2023-04-29  发布在  Node.js
关注(0)|答案(1)|浏览(108)

我有一个Remix全栈应用程序,它使用S3存储桶来存储和检索图像。当本地运行应用程序时,将图像从应用程序上传到S3可以按预期工作。但是,在部署应用程序towww.example之后 www.example.com :“无法向示例发出HTTP请求:连接错误:超时”。我试过改变S3 Jmeter 板上的权限和其他设置,但似乎没有任何帮助。
这是我的bucket政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:PutObjectAcl",
                "s3:PutObject",
                "s3:GetObject"
            ],
            "Resource": "arn:aws:s3:::BUCKETNAME/*"
        }
    ]
}

这是我在S3存储桶上的CORS配置:

[
    {
        "AllowedHeaders": [
            "*"
        ],
        "AllowedMethods": [
            "GET",
            "HEAD",
            "PUT",
            "POST"
        ],
        "AllowedOrigins": [
            "*"
        ],
        "ExposeHeaders": [],
        "MaxAgeSeconds": 3000
    }
]

存储桶当前已启用公共访问(Block all public access off),Object Ownership设置为“Bucket owner enforced”。任何帮助将不胜感激。
我的动作也有很多其他的东西,但这里是调用上传函数的部分:

const uploadHandler = unstable_composeUploadHandlers(
    async ({ name, contentType, data, filename }) => {
      if (name !== 'image') {
        return undefined;
      }

      const dataArray1 = [];
      for await (const x of data) {
        dataArray1.push(x);
      }

      const stream = Readable.from(dataArray1);

      const uploadImageService = new UploadImageService(ctx);
      try {
        return await uploadImageService.uploadImage(
          stream,
          folder,
          `${id}-${itemName}.jpg`
        );
      } catch (error) {
        throw serverError({ code: 'image_upload_failed' });
      }
    },
    unstable_createFileUploadHandler()
  );

  let image: FormDataEntryValue | null = null;
  const multipartFormData = await unstable_parseMultipartFormData(request, uploadHandler);
  image = multipartFormData.get('image');

这是服务方法:

async uploadImage(file: Readable, folder?: string, tag?: string): Promise<string> {
    const { s3 } = getS3();

    return await uploadImageToS3(s3, 'BUCKETNAME', file, folder, tag);
  }

这是它实际上传的地方:

export const uploadImageToS3 = async (
  s3: AWS.S3,
  s3Bucket: string,
  buffer: any,
  folder?: string,
  tag?: string
): Promise<string> => {
  const filename = folder ? `${folder}/${tag || uuid()}` : tag || uuid();

  const params = {
    Bucket: s3Bucket,
    Key: filename,
    Body: buffer,
    ContentType: 'image/jpeg',
  };

  try {
    // this is where it times out, it never reaches the console.log() success message below when the app is deployed
    const s3UploadResponse = await s3.upload(params).promise();
    console.log(`File uploaded successfully to ${s3UploadResponse.Location}`);

    return new URL(s3UploadResponse.Location).pathname.substring(1);
  } catch (error) {
    console.error(error);
    throw new Error('File upload failed.');
  }
};
xzv2uavs

xzv2uavs1#

在无法在代码或AWS/Fly设置中找到导致超时的任何内容后,我在本地运行项目时注意到以下消息:
(node:14152)注:我们正在正式制定计划,在2023年将AWS SDK for JavaScript(v2)进入维护模式。
请迁移您的代码以使用AWS SDK for JavaScript(v3)。有关详细信息,请查看https://a.co/7PzMCcy上的迁移指南
所以我在使用AWS SDK的每个文件上运行了这个命令:

npx aws-sdk-js-codemod -t v2-to-v3 example.ts

这对文件进行了一些更改,然后我需要安装更多的AWS依赖项,但在更新完成后,超时不再发生,所以上传图像现在可以在本地和部署服务器上工作。

相关问题