Creating AWS S3 object life cycle using NodeJS

880 Views Asked by At

Creating AWS S3 object life cycle using NodeJS.

I want to create S3 object life cycle via API using NodeJS. When I see the documentation, AWS provided only multiple object life cycle, with Java.

https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html

I also checked this url -

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getBucketLifecycle-property

Genral Concern

How to set multiple Transition with NodeJS like the way Java has ?

BucketLifecycleConfiguration.Rule rule2 = new BucketLifecycleConfiguration.Rule()
                .withId("Archive and then delete rule")
                .withFilter(new LifecycleFilter(new LifecycleTagPredicate(new Tag("archive", "true"))))
                .addTransition(new Transition().withDays(30).withStorageClass(StorageClass.StandardInfrequentAccess))
                .addTransition(new Transition().withDays(365).withStorageClass(StorageClass.Glacier))
                .withExpirationInDays(3650)
                .withStatus(BucketLifecycleConfiguration.ENABLED);

Followed by - https://docs.aws.amazon.com/AmazonS3/latest/userguide/how-to-set-lifecycle-configuration-intro.html

Any help would be great.

1

There are 1 best solutions below

1
On

we need to call putBucketLifecycle and pass Rules Array to LifecycleConfiguration. Similar to CLI Example

s3.putBucketLifecycle(
  {
    Bucket: "sample-temp-bucket",
    LifecycleConfiguration: {
      Rules: [
        {
          Filter: {
            And: {
              Prefix: "myprefix",
              Tags: [
                {
                  Value: "mytagvalue1",
                  Key: "mytagkey1",
                },
                {
                  Value: "mytagvalue2",
                  Key: "mytagkey2",
                },
              ],
            },
          },
          Status: "Enabled",
          Expiration: {
            Days: 1,
          },
        },
        {
          Filter: {
            Prefix: "documents/",
          },
          Status: "Enabled",
          Transitions: [
            {
              Days: 365,
              StorageClass: "GLACIER",
            },
          ],
          Expiration: {
            Days: 3650,
          },
          ID: "ExampleRule",
        },
      ],
    },
  },
  (error, result) => {
    if (error) console.log("error", error);
    if (result) console.log("result", result);
  }
);