aws-s3-presigned-url-generator
v1.0.4
Published
This package provides utility functions to generate presigned URLs for uploading, retrieving, and listing objects in an AWS S3 bucket. Using AWS SDK for JavaScript (`@aws-sdk/client-s3`), it allows you to perform the following actions:
Readme
S3 Presigned URL Utility
This package provides utility functions to generate presigned URLs for uploading, retrieving, and listing objects in an AWS S3 bucket. Using AWS SDK for JavaScript (@aws-sdk/client-s3), it allows you to perform the following actions:
- Upload objects to S3 using a presigned URL.
- Retrieve objects from S3 using a presigned URL.
- List objects in a bucket using a presigned URL.
Features
- Generate presigned URL for uploading files to S3.
- Generate presigned URL for downloading files from S3.
- Generate presigned URL to list objects in a specific S3 path.
- Specify expiration time for presigned URLs (defaults to 24 hours).
Installation
Before using the utility, ensure you have installed the necessary dependencies:
npm install @aws-sdk/client-s3 @aws-sdk/s3-request-presignerUsage
1. Generate a Presigned URL for Uploading an Object
import { createPresignedUrlWithClient } from './path/to/utility';
import { S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({ region: 'us-east-1' });
const uploadUrl = await createPresignedUrlWithClient({
s3Client,
bucket: 'my-bucket',
key: 'uploads/myfile.png',
expiresIn: 3600, // URL valid for 1 hour
contentType: 'image/png'
});
2. Generate a Presigned URL for Downloading an Object
import { createSignedUrlForGetObject } from './path/to/utility';
import { S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({ region: 'us-east-1' });
const downloadUrl = await createSignedUrlForGetObject({
s3Client,
bucket: 'my-bucket',
key: 'uploads/myfile.png',
expiresIn: 3600 // URL valid for 1 hour
});
3. Generate a Presigned URL for Listing Objects in a Path
import { getBatchPresignedUrlsWithClient } from './path/to/utility';
import { S3Client } from '@aws-sdk/client-s3';
const s3Client = new S3Client({ region: 'us-east-1' });
const listUrl = await getBatchPresignedUrlsWithClient({
s3Client,
bucket: 'my-bucket',
pathPrefix: 'uploads/',
expiresIn: 3600 // URL valid for 1 hour
});
API Reference
createPresignedUrlWithClient(options)
Generates a presigned URL for uploading an object to an S3 bucket.
Parameters:
s3Client(required): An instance ofS3Clientfrom@aws-sdk/client-s3.bucket(required): The name of the S3 bucket.key(required): The object key (e.g.,myfolder/myfile.png).expiresIn(optional): Expiration time for the presigned URL in seconds (default is 24 hours).
Returns: A promise that resolves to the presigned URL.
createSignedUrlForGetObject(options)
Generates a presigned URL for downloading an object from an S3 bucket.
Parameters:
s3Client(required): An instance ofS3Clientfrom@aws-sdk/client-s3.bucket(required): The name of the S3 bucket.key(required): The object key (e.g.,myfolder/myfile.png).expiresIn(optional): Expiration time for the presigned URL in seconds (default is 24 hours).
Returns: A promise that resolves to the presigned URL.
getBatchPresignedUrlsWithClient(options)
Generates a presigned URL for listing objects in an S3 bucket.
Parameters:
s3Client(required): An instance ofS3Clientfrom@aws-sdk/client-s3.bucket(required): The name of the S3 bucket.pathPrefix(optional): The prefix for listing objects (e.g.,uploads/).expiresIn(optional): Expiration time for the presigned URL in seconds (default is 24 hours).
Returns: A promise that resolves to the presigned URL.
Types
ICreatePresignedUrlWithClientBody
Defines the shape of the request body for generating presigned URLs for upload and download.
interface ICreatePresignedUrlWithClientBody {
s3Client: S3Client;
bucket: string;
key: string;
expiresIn?: number;
}IGetBatchPresignedUrlWithClientBody
Defines the shape of the request body for generating presigned URLs for listing objects in a bucket.
interface IGetBatchPresignedUrlWithClientBody extends ICreatePresignedUrlWithClientBody {
pathPrefix?: string;
}