Search code examples
typescriptamazon-s3aws-sdkaws-sdk-jsaws-sdk-nodejs

Make S3 Object public when uploaded by client-s3 Typescript library


I am uploading files to S3 using Typescript code as follows:

    import {PutObjectCommand, PutObjectCommandInput, S3Client} from "@aws-sdk/client-s3";
    const s3 = new S3Client(config);
    const uploadParams: PutObjectCommandInput = {
        Bucket: bucketName,
        Key: filename,
        Body: fileData
    };
 
    await s3.send(new PutObjectCommand(uploadParams));

However, I need to make object public after uploading in order to access it as a part of a static website and it is uploaded as private. How I can tell the s3 client to also make it public?


Solution

  • In uploadParams, you can supply parameters described in PutObjectRequest, including ACL.

    Or, after upload, you can use PutObjectAclCommand and the public-read canned ACL.

    An alternative is to simply make all objects within the S3 bucket public via S3 bucket policy, so you don't have to make each and every object public individually. This assumes that all objects in the bucket are designed to be publicly readable, of course, as is often the case with static sites.