使用 CDK 上传文件到 S3 bucket

Uploading files to S3 bucket using CDK

我正在尝试将文件上传到使用 CDK 创建的 S3 存储桶,但是即使使用 AWS 提供的示例代码,我也一直 运行 遇到同样的错误。

这是堆栈。

export class TestStack extends cdk.Stack {
    public readonly response: string;

    constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
        super(scope, id, props);

        const websiteBucket = new s3.Bucket(this, 'WebsiteBucket', {
            websiteIndexDocument: 'index.html',
            publicReadAccess: true,
        });
        new s3deploy.BucketDeployment(this, 'DeployWebsite', {
            sources: [s3deploy.Source.asset('./website-dist')],
            destinationBucket: websiteBucket,
            destinationKeyPrefix: 'web/static' // optional prefix in destination bucket
        });

    }
}

这是要求的主垃圾箱

const app = new cdk.App();

new TestStack(app, "TestStack", {
  env: {
    account: '123456789',
    region: 'us-east-1',
  }
});

destinationBucket 在 visual studio 代码中和 运行 npm build

时提供以下错误

Property 'env' is missing in type 'Bucket' but required in type 'IBucket'.

据我所知,env 与此处描述的资源环境相关:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_core.ResourceEnvironment.html

我不知道如何将它提供给存储桶对象,因为它似乎没有出现在此处描述的可用输入道具中:https://docs.aws.amazon.com/cdk/api/latest/docs/@aws-cdk_aws-s3.Bucket.html

如有任何帮助,我们将不胜感激。

尝试将 env 传递到 Bucket Stack 道具中。

代码片段:

贴花堆叠时,它应该位于 bin 文件夹下。


const myEnv = {
    account: '123456789',
    region: 'us-east-1',
  };
  
const App = new cdk.App();

new S3Stack(App, 'S3Stack',{env: myEnv}); 

您可能应该为描述的 CLI 配置您的环境 in the cli guide