如果我为静态网站托管配置一个s3 bucket,那么我可以通过API向它上传资产吗?

If I configure a s3 bucket for static website hosting, then can I upload assets to it through API?

我有一个为静态网站托管配置的 s3 存储桶,现在我经常需要更改我网站的资产, 我已经有一个自定义工具可以将资产上传到我的其他存储桶是否可以通过 AWS 将文件上传到我的 s3 存储桶API?

是的,这是一种完全可以接受的上传新资产的方式,我不是这样做的,但它应该工作得很好,最终无论你选择上传那些资产,很可能在幕后它使用的是相同的API 无论如何都会打电话。

您可以使用 AWS-SDK 上传到 S3 存储桶。

NPM 包:https://www.npmjs.com/package/aws-sdk

            // Load the AWS SDK for Node.js
            var AWS = require('aws-sdk');
            // Set the region 
            AWS.config.update({ region: 'REGION' });

            // Create S3 service object
            s3 = new AWS.S3({ apiVersion: '2006-03-01' });

            const s3 = new AWS.S3();
            const params = {
                Bucket: S3_BUCKET, //bucket name
                Key: `${s3Folder}/${filename}`, // type is not required
                Body: base64, //image base64
                ACL: 'public-read',
                ContentEncoding: 'base64', // required
                ContentType: `image/${fileType}` // required. Notice the back ticks
            }

            let location = '';
            let key = '';
            try {
                const { Location, Key } = await s3.upload(params).promise();
                location = Location;
                key = Key;
                console.log({ location, key })
            } catch (error) {
                console.log("Error", error)
            }

阅读更多@AWS 文档:https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/getting-started-nodejs.html