UNPKG

node-cloudflare-r2

Version:
118 lines (117 loc) 4.65 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.R2 = void 0; const client_s3_1 = require("@aws-sdk/client-s3"); const Bucket_1 = require("./Bucket"); class R2 { constructor(config, overrides) { this.config = config; if (overrides === null || overrides === void 0 ? void 0 : overrides.endpoint) { this.endpoint = overrides.endpoint; } else if (this.config.jurisdiction) { this.endpoint = `https://${this.config.accountId}.${this.config.jurisdiction}.r2.cloudflarestorage.com`; } else { this.endpoint = `https://${this.config.accountId}.r2.cloudflarestorage.com`; } this.r2 = new client_s3_1.S3Client(Object.assign({ endpoint: this.endpoint, credentials: { accessKeyId: this.config.accessKeyId, secretAccessKey: this.config.secretAccessKey, }, region: 'auto' }, overrides)); } /** * Returns a `Bucket` object that represents the specified storage bucket. * @param bucketName The name of the storage bucket. * @returns A `Bucket` object that represents the specified storage bucket. */ bucket(bucketName) { return new Bucket_1.Bucket(this.r2, bucketName, this.endpoint); } /** * Returns a list of all buckets owned by the authenticated sender of the request. * @async */ listBuckets() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const result = yield this.r2.send(new client_s3_1.ListBucketsCommand({})); const buckets = ((_a = result.Buckets) === null || _a === void 0 ? void 0 : _a.map((bucket) => { return { name: bucket.Name, creationDate: bucket.CreationDate, }; })) || []; const owner = { id: (_b = result.Owner) === null || _b === void 0 ? void 0 : _b.ID, displayName: (_c = result.Owner) === null || _c === void 0 ? void 0 : _c.DisplayName, }; return { buckets, owner }; }); } /** * Determines if a bucket exists and you have permission to access it. * @async * @param bucketName */ bucketExists(bucketName) { return __awaiter(this, void 0, void 0, function* () { return yield this.bucket(bucketName).exists(); }); } /** * Create a new R2 bucket and returns `Bucket` object. * @async * @param bucketName */ createBucket(bucketName) { return __awaiter(this, void 0, void 0, function* () { yield this.r2.send(new client_s3_1.CreateBucketCommand({ Bucket: bucketName, })); return new Bucket_1.Bucket(this.r2, bucketName, this.endpoint); }); } /** * Delete an existing bucket. Returns true if success or throws error if fail. * @async * @param bucketName */ deleteBucket(bucketName) { return __awaiter(this, void 0, void 0, function* () { const result = yield this.r2.send(new client_s3_1.DeleteBucketCommand({ Bucket: bucketName, })); return result.$metadata.httpStatusCode === 204; }); } /** * Returns Cross-Origin Resource Sharing (CORS) policies of the bucket. * @async */ getBucketCors(bucketName) { return __awaiter(this, void 0, void 0, function* () { return yield this.bucket(bucketName).getCors(); }); } /** * Returns the region the bucket resides in. For `Cloudflare R2`, the region is always `auto`. * @async * @param bucketName */ getBucketRegion(bucketName) { return __awaiter(this, void 0, void 0, function* () { return yield this.bucket(bucketName).getRegion(); }); } } exports.R2 = R2;