Skip to content

It is possible to interact with Lightsail Bucket using S3Client?

0

It is possible to interact with Lightsail Bucket using S3Client? or is S3Client designed to interact only with S3 buckets?

asked 2 years ago354 views
1 Answer
1
Accepted Answer

Hello.

You probably can't create Lightsail buckets with S3Client.
Lightsail buckets have an API for that.
https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/lightsail/command/CreateBucketCommand/

If you are uploading files, etc., you may be able to use S3Client.
It seems that creating a bucket uses the Lightsail API and uploading objects using the S3 API.
https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-uploading-files-to-a-bucket.html#uploading-files-to-a-bucket-aws-cli

EXPERT
answered 2 years ago
EXPERT
reviewed 2 years ago
  • In the case of a Lightsail bucket, uploading was not possible unless an access key and secret access key were issued. So, please follow the steps described in the document below to issue an access key, set it in your code, and run it. I ran the code below with Lambda and was able to upload the file to a Lightsail bucket. https://docs.aws.amazon.com/lightsail/latest/userguide/amazon-lightsail-creating-bucket-access-keys.html

    import { PutObjectCommand, S3Client } from "@aws-sdk/client-s3";
    
    const client = new S3Client({
        credentials: {
        accessKeyId: "AccessKey",
        secretAccessKey: "SecretAccessKey",
      }});
    
    export const handler = async (event) => {
      const command = new PutObjectCommand({
        Bucket: "bucket-name",
        Key: "hello-s3.txt",
        Body: "Hello S3!",
      });
    
      try {
        const response = await client.send(command);
        console.log(response);
        return {
          statusCode: 200,
          body: JSON.stringify(response),
        };
      } catch (err) {
        console.error(err);
        return {
          statusCode: 500,
          body: JSON.stringify(err),
        };
      }
    };
    
    

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.