Create a URL with an AWS S3 expiration date and download files in S3

Create a URL with an AWS S3 expiration date and download files in S3

This is a very nice feature that allows you to download a file in AWS S3 with a ttl in the URL called due date URL, which can be accessed at any valid time to download the specified file.

Below, we will create a due date URL for the zip file in the bucket and send it to the email address specified in the SNS topic, Subscription.

const aws = require('aws-sdk');

exports.handler = function(event, context){
  const s3 = new aws.S3();
  const params = {Bucket: 'sig-v4', Key: 'SSL.zip', Expires: 60};
  const url = s3.getSignedUrl('getObject', params);

  const sns = new aws.SNS();
  sns.publish(
    {
      TopicArn: 'arn:aws:sns:us-xxxxx:xxxxxxxxxxxx:test',
      Message: url,
      Subject: 'タイトル'
    }, function (err, data) {
    if (!err) {
      context.done(null,'');
    }else{
      context.fail();
    }
  });
};

This will be sent to the specified e-mail, and when you click on the URL, the file SSL.zip, as mentioned above, will be downloaded.

The expiration time unit is “seconds,” so in this sample it is 60 seconds.

If you access the URL after 60 seconds, you will see the following screen.

AWS S3の期限付きURLを作成し、S3にあるファイルをダウンロードする

コメント

Discover more from 株式会社CONFRAGE ITソリューション事業部

Subscribe now to keep reading and get access to the full archive.

Continue reading

Copied title and URL