上传到AWS S3时,Strapi损坏了图像

发布于 2025-01-29 06:04:28 字数 862 浏览 4 评论 0原文

我安装了AWS S3作为我的strapi后端的上传提供商:

// path: ./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'aws-s3',
      providerOptions: {
        accessKeyId: env('AWS_ACCESS_KEY_ID'),
        secretAccessKey: env('AWS_ACCESS_SECRET'),
        region: env('AWS_REGION'),
        params: {
          Bucket: env('AWS_BUCKET'),
        },
      },
    },
  },
  // ...
});

它已连接到AWS S3,但是当我上传时,图像已损坏:

我仍然将图像上传到AWS S3:

我不知道该如何解决此问题,因为我是使用Strapi和AWS S3的新手。感谢您的帮助!

I installed aws s3 as an upload provider for my Strapi Backend:

// path: ./config/plugins.js

module.exports = ({ env }) => ({
  // ...
  upload: {
    config: {
      provider: 'aws-s3',
      providerOptions: {
        accessKeyId: env('AWS_ACCESS_KEY_ID'),
        secretAccessKey: env('AWS_ACCESS_SECRET'),
        region: env('AWS_REGION'),
        params: {
          Bucket: env('AWS_BUCKET'),
        },
      },
    },
  },
  // ...
});

It is connected to aws s3 but the image is broken when I uploaded it:
Broken Image

And I still get the image uploaded to aws s3:
AWS S3 Uploaded

I don't know how to fix this because I'm new to using Strapi and AWS S3. Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独享拥抱 2025-02-05 06:04:28

您需要在./ config/middlewares.js中替换strapi ::安全字符串,但以下内容:

 {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
          directives: {
            'connect-src': ["'self'", 'https:'],
            'img-src': ["'self'", 'data:', 'blob:', `${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`],
            'media-src': ["'self'", 'data:', 'blob:', `${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`],
            upgradeInsecureRequests: null,
        },
      },
    },
  },

You need to replace strapi::security string in ./config/middlewares.js with the following:

 {
    name: 'strapi::security',
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
          directives: {
            'connect-src': ["'self'", 'https:'],
            'img-src': ["'self'", 'data:', 'blob:', `${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`],
            'media-src': ["'self'", 'data:', 'blob:', `${process.env.AWS_BUCKET}.s3.${process.env.AWS_REGION}.amazonaws.com`],
            upgradeInsecureRequests: null,
        },
      },
    },
  },
梨涡 2025-02-05 06:04:28

补充我们的朋友上面评论的内容,请从config/midderwares.js文件上删除AWS_Region

。示例文件看起来像这样:

const BUCKET = process.env.AWS_BUCKET;
const BUCKET_URL = `https://${BUCKET}.s3.amazonaws.com`;

module.exports = [
  "strapi::errors",
  "strapi::security",
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": ["'self'", "data:", "blob:", BUCKET_URL],
          "media-src": ["'self'", "data:", "blob:", BUCKET_URL],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
];

Complementing that our friend commented above,remove AWS_REGION from url on config/middlewares.js file

The example file looks like this:

const BUCKET = process.env.AWS_BUCKET;
const BUCKET_URL = `https://${BUCKET}.s3.amazonaws.com`;

module.exports = [
  "strapi::errors",
  "strapi::security",
  "strapi::cors",
  "strapi::poweredBy",
  "strapi::logger",
  "strapi::query",
  "strapi::body",
  "strapi::session",
  "strapi::favicon",
  "strapi::public",
  {
    name: "strapi::security",
    config: {
      contentSecurityPolicy: {
        useDefaults: true,
        directives: {
          "connect-src": ["'self'", "https:"],
          "img-src": ["'self'", "data:", "blob:", BUCKET_URL],
          "media-src": ["'self'", "data:", "blob:", BUCKET_URL],
          upgradeInsecureRequests: null,
        },
      },
    },
  },
];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文