首页 > 解决方案 > 使用 CDK 将文件上传到 S3 存储桶

问题描述

我正在尝试将文件上传到使用 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',
  }
});

destinationBucketVisual 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

任何帮助将不胜感激。

标签: typescriptamazon-web-servicesamazon-s3aws-cdk

解决方案


尝试传入envBucket Stack 道具。

代码片段:

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


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

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


推荐阅读