Retrieve aws Glacier data using lambda functions

56 Views Asked by At

I am new to aws glacier storage class, we can retrieve the data from aws cli commands, but how can we use the aws lambda function node js sdk to initiate the job and give the url of the retrieved archive from aws glacier at client side

1

There are 1 best solutions below

0
helper On

To initiate an AWS Glacier retrieval job using AWS Lambda function and Node.js SDK, you can use the following code snippet: const AWS = require('aws-sdk');

exports.handler = async (event, context) => {
  const glacier = new AWS.Glacier();

  const vaultName = 'my-vault';
  const archiveId = 'my-archive-id';
  const jobParams = {
    Type: 'archive-retrieval',
    ArchiveId: archiveId,
    Description: 'My retrieval job',
    SNSTopic: 'my-sns-topic-arn'
    // you can include other job parameters as needed
  }

  const result = await glacier.initiateJob({
    vaultName: vaultName,
    jobParameters: jobParams
  }).promise();

  return {
    statusCode: 200,
    body: JSON.stringify({
      jobId: result.jobId
    })
  };
};

This code uses the Node.js AWS SDK to initiate a retrieval job for a specific archive in an AWS Glacier vault. You can customize the job parameters according to your requirements. Once the job is initiated, the Lambda function returns a JSON object containing the job ID. To provide the URL of the retrieved archive to the client, you can use an Amazon SNS topic to send a notification when the retrieval job is completed. You can configure the SNS topic to trigger a Lambda function that generates a pre-signed URL for the retrieved archive and returns it to the client. Here's some sample code that you can use in your Lambda function to generate a pre-signed URL:

const signedUrl = await glacier.getJobOutput({
  vaultName: vaultName,
  jobId: jobId
}).promise().then((data) => {
  const downloadUrl = `https://${vaultName}.s3.amazonaws.com/${archiveId}`;
  const signedUrl = s3.getSignedUrl('getObject', {
    Bucket: vaultName,
    Key: archiveId,
    Expires: 3600 // URL expiry time in seconds
  });
  return signedUrl;
});

This code uses the getJobOutput method of the AWS Glacier SDK to retrieve the archive from Glacier and then generates a pre-signed URL for it using the AWS S3 SDK. You can then return this URL to the client.