UNPKG

cloud-presign

Version:

A unified presigned URL generator for multiple cloud platforms

97 lines (96 loc) 4.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureProvider = void 0; const storage_blob_1 = require("@azure/storage-blob"); const types_1 = require("../types"); const common_1 = require("./common"); class AzureProvider { constructor(config, defaultExpiration) { this.common = new common_1.Common(); this.containerName = config.container; this.accountName = config.accountName; this.accountKey = config.accountKey; this.defaultExpiration = defaultExpiration; // Create the BlobServiceClient if (config.connectionString) { this.blobServiceClient = storage_blob_1.BlobServiceClient.fromConnectionString(config.connectionString); } else { const credential = new storage_blob_1.StorageSharedKeyCredential(config.accountName, config.accountKey); const blobServiceUri = `https://${config.accountName}.blob.core.windows.net`; this.blobServiceClient = new storage_blob_1.BlobServiceClient(blobServiceUri, credential); } } async generatePresignedUrl(params, options) { const expiration = (options === null || options === void 0 ? void 0 : options.expiration) || this.defaultExpiration; const method = params.method || types_1.HttpMethod.GET; let fileName = params.key; let key = params.key; // Create permissions based on HTTP method const permissions = new storage_blob_1.BlobSASPermissions(); switch (method) { case types_1.HttpMethod.GET: permissions.read = true; break; case types_1.HttpMethod.PUT: case types_1.HttpMethod.POST: permissions.create = true; permissions.write = true; fileName = await this.common.generateFileUniqueName(params.key); key = `${params.prefix}/${fileName}`; break; case types_1.HttpMethod.DELETE: permissions.delete = true; break; default: throw new Error(`Unsupported HTTP method: ${method}`); } // Set start and expiry time const startDate = new Date(); const expiryDate = new Date(startDate); expiryDate.setSeconds(startDate.getSeconds() + expiration); // Define SAS parameters const sasOptions = { containerName: this.containerName, blobName: params.key, permissions, startsOn: startDate, expiresOn: expiryDate, }; // Create the shared key credential const sharedKeyCredential = new storage_blob_1.StorageSharedKeyCredential(this.accountName, this.accountKey); // Generate the SAS token const sasToken = (0, storage_blob_1.generateBlobSASQueryParameters)(sasOptions, sharedKeyCredential).toString(); // Construct the full URL const containerClient = this.blobServiceClient.getContainerClient(this.containerName); const blobClient = containerClient.getBlobClient(fileName); const presignedUrl = `${blobClient.url}?${sasToken}`; const url = blobClient.url.split('?')[0]; // Calculate expiration date const expiresAt = expiryDate.toISOString(); // Build response const response = { key, presignedUrl, url: params.isPublic ? url : '', method, expiresAt, headers: {}, }; // Add content type header for upload requests if ((method === types_1.HttpMethod.PUT || method === types_1.HttpMethod.POST) && params.contentType) { response.headers['Content-Type'] = params.contentType; } return response; } async makeFilePublic() { const containerClient = this.blobServiceClient.getContainerClient(this.containerName); // Check current access level const properties = await containerClient.getProperties(); if (properties.blobPublicAccess !== 'blob') { // Set container access level to 'blob' (public read access to blobs) await containerClient.setAccessPolicy('blob'); } } } exports.AzureProvider = AzureProvider;