@tweedegolf/sab-adapter-amazon-s3
Advanced tools
Comparing version 1.0.15 to 1.0.17
@@ -0,1 +1,9 @@ | ||
# 1.0.17 | ||
- add support for optional key `useSignedUrl` in options object `getFileAsUrl` | ||
# 1.0.16 | ||
- updated @aws-sdk to 3.582.0 | ||
# 1.0.15 | ||
@@ -2,0 +10,0 @@ |
"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 }); | ||
@@ -58,16 +49,10 @@ exports.AbstractAdapter = void 0; | ||
} | ||
addFileFromPath(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.addFile(params); | ||
}); | ||
async addFileFromPath(params) { | ||
return await this.addFile(params); | ||
} | ||
addFileFromBuffer(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.addFile(params); | ||
}); | ||
async addFileFromBuffer(params) { | ||
return await this.addFile(params); | ||
} | ||
addFileFromStream(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return yield this.addFile(params); | ||
}); | ||
async addFileFromStream(params) { | ||
return await this.addFile(params); | ||
} | ||
@@ -99,224 +84,204 @@ _getFileAndBucket(arg1, arg2) { | ||
// public | ||
clearBucket(name) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async clearBucket(name) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
name = this._bucketName; | ||
} | ||
return this._clearBucket(name); | ||
}); | ||
name = this._bucketName; | ||
} | ||
return this._clearBucket(name); | ||
} | ||
deleteBucket(name) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async deleteBucket(name) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
name = this._bucketName; | ||
} | ||
return this._deleteBucket(name); | ||
}); | ||
name = this._bucketName; | ||
} | ||
return this._deleteBucket(name); | ||
} | ||
bucketExists(name) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async bucketExists(name) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
if (typeof name === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
name = this._bucketName; | ||
} | ||
return this._bucketExists(name); | ||
}); | ||
name = this._bucketName; | ||
} | ||
return this._bucketExists(name); | ||
} | ||
listFiles(arg1, arg2) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async listFiles(arg1, arg2) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
let bucketName; | ||
let numFiles = 10000; | ||
if (typeof arg1 === "number") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
let bucketName; | ||
let numFiles = 10000; | ||
if (typeof arg1 === "number") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
bucketName = this._bucketName; | ||
numFiles = arg1; | ||
bucketName = this._bucketName; | ||
numFiles = arg1; | ||
} | ||
else if (typeof arg1 === "string") { | ||
bucketName = arg1; | ||
if (typeof arg2 === "number") { | ||
numFiles = arg2; | ||
} | ||
else if (typeof arg1 === "string") { | ||
bucketName = arg1; | ||
if (typeof arg2 === "number") { | ||
numFiles = arg2; | ||
} | ||
} | ||
else { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
else { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
bucketName = this._bucketName; | ||
} | ||
return this._listFiles(bucketName, numFiles); | ||
}); | ||
bucketName = this._bucketName; | ||
} | ||
return this._listFiles(bucketName, numFiles); | ||
} | ||
addFile(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async addFile(params) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
if (typeof params.bucketName === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
if (typeof params.bucketName === "undefined") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
params.bucketName = this._bucketName; | ||
} | ||
if (typeof params.options !== "object") { | ||
params.options = {}; | ||
} | ||
return this._addFile(params); | ||
}); | ||
params.bucketName = this._bucketName; | ||
} | ||
if (typeof params.options !== "object") { | ||
params.options = {}; | ||
} | ||
return this._addFile(params); | ||
} | ||
getFileAsStream(arg1, arg2, arg3) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.configError !== null) { | ||
return { error: this.configError, value: null }; | ||
async getFileAsStream(arg1, arg2, arg3) { | ||
if (this.configError !== null) { | ||
return { error: this.configError, value: null }; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let options = {}; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 !== "undefined") { | ||
options = arg3; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let options = {}; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 !== "undefined") { | ||
options = arg3; | ||
} | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 !== "undefined") { | ||
options = arg2; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 !== "undefined") { | ||
options = arg2; | ||
} | ||
return this._getFileAsStream(bucketName, fileName, options); | ||
}); | ||
} | ||
return this._getFileAsStream(bucketName, fileName, options); | ||
} | ||
getFileAsURL(arg1, arg2, arg3) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async getFileAsURL(arg1, arg2, arg3) { | ||
if (this._configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let options = {}; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 !== "undefined") { | ||
options = arg3; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let options = {}; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 !== "undefined") { | ||
options = arg3; | ||
} | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "no bucket selected", | ||
}; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 !== "undefined") { | ||
options = arg2; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 !== "undefined") { | ||
options = arg2; | ||
} | ||
return this._getFileAsURL(bucketName, fileName, options); | ||
}); | ||
} | ||
return this._getFileAsURL(bucketName, fileName, options); | ||
} | ||
sizeOf(arg1, arg2) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
const { bucketName, fileName, error } = this._getFileAndBucket(arg1, arg2); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
return this._sizeOf(bucketName, fileName); | ||
}); | ||
async sizeOf(arg1, arg2) { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
const { bucketName, fileName, error } = this._getFileAndBucket(arg1, arg2); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
return this._sizeOf(bucketName, fileName); | ||
} | ||
fileExists(arg1, arg2) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
const { bucketName, fileName, error } = this._getFileAndBucket(arg1, arg2); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
return this._fileExists(bucketName, fileName); | ||
}); | ||
async fileExists(arg1, arg2) { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
const { bucketName, fileName, error } = this._getFileAndBucket(arg1, arg2); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
return this._fileExists(bucketName, fileName); | ||
} | ||
removeFile(arg1, arg2, arg3) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async removeFile(arg1, arg2, arg3) { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let allVersions = false; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 === "boolean") { | ||
allVersions = arg3; | ||
} | ||
let bucketName; | ||
let fileName; | ||
let allVersions = false; | ||
if (typeof arg1 === "string" && typeof arg2 === "string") { | ||
bucketName = arg1; | ||
fileName = arg2; | ||
if (typeof arg3 === "boolean") { | ||
allVersions = arg3; | ||
} | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "No bucket selected", | ||
}; | ||
} | ||
else if (typeof arg1 === "string" && typeof arg2 !== "string") { | ||
if (this._bucketName === null) { | ||
return { | ||
value: null, | ||
error: "No bucket selected", | ||
}; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 === "boolean") { | ||
allVersions = arg2; | ||
} | ||
bucketName = this._bucketName; | ||
fileName = arg1; | ||
if (typeof arg2 === "boolean") { | ||
allVersions = arg2; | ||
} | ||
return this._removeFile(bucketName, fileName, allVersions); | ||
}); | ||
} | ||
return this._removeFile(bucketName, fileName, allVersions); | ||
} | ||
@@ -323,0 +288,0 @@ } |
"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()); | ||
}); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -28,3 +19,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
if (typeof config !== "string") { | ||
this._config = Object.assign({}, config); | ||
this._config = { ...config }; | ||
} | ||
@@ -39,3 +30,3 @@ else { | ||
if (searchParams !== null) { | ||
this._config = Object.assign({ type }, searchParams); | ||
this._config = { type, ...searchParams }; | ||
} | ||
@@ -59,13 +50,16 @@ else { | ||
if (this.config.accessKeyId && this.config.secretAccessKey) { | ||
const o = Object.assign({}, this.config); // eslint-disable-line | ||
const o = { ...this.config }; // eslint-disable-line | ||
delete o.credentials; | ||
delete o.accessKeyId; | ||
delete o.secretAccessKey; | ||
this._client = new client_s3_1.S3Client(Object.assign({ credentials: { | ||
this._client = new client_s3_1.S3Client({ | ||
credentials: { | ||
accessKeyId: this.config.accessKeyId, | ||
secretAccessKey: this.config.secretAccessKey, | ||
} }, o)); | ||
}, | ||
...o, | ||
}); | ||
} | ||
else { | ||
const o = Object.assign({}, this.config); // eslint-disable-line | ||
const o = { ...this.config }; // eslint-disable-line | ||
delete o.accessKeyId; | ||
@@ -83,98 +77,82 @@ delete o.secretAccessKey; | ||
} | ||
getFiles(bucketName_1) { | ||
return __awaiter(this, arguments, void 0, function* (bucketName, maxFiles = 10000) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
MaxKeys: maxFiles, | ||
}; | ||
const command = new client_s3_1.ListObjectsCommand(input); | ||
const { Contents } = yield this._client.send(command); | ||
// console.log("Contents", Contents); | ||
return { value: Contents, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async getFiles(bucketName, maxFiles = 10000) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
MaxKeys: maxFiles, | ||
}; | ||
const command = new client_s3_1.ListObjectsCommand(input); | ||
const { Contents } = await this._client.send(command); | ||
// console.log("Contents", Contents); | ||
return { value: Contents, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
getFileVersions(bucketName_1) { | ||
return __awaiter(this, arguments, void 0, function* (bucketName, maxFiles = 10000) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
MaxKeys: maxFiles, | ||
}; | ||
const command = new client_s3_1.ListObjectVersionsCommand(input); | ||
const { Versions } = yield this._client.send(command); | ||
// console.log("Versions", Versions); | ||
return { value: Versions, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async getFileVersions(bucketName, maxFiles = 10000) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
MaxKeys: maxFiles, | ||
}; | ||
const command = new client_s3_1.ListObjectVersionsCommand(input); | ||
const { Versions } = await this._client.send(command); | ||
// console.log("Versions", Versions); | ||
return { value: Versions, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
// protected, called by methods of public API via AbstractAdapter | ||
_getFileAsStream(bucketName, fileName, options) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { start, end } = options; | ||
let range = `bytes=${start}-${end}`; | ||
if (typeof start === "undefined" && typeof end === "undefined") { | ||
range = undefined; | ||
} | ||
else if (typeof start === "undefined") { | ||
range = `bytes=0-${end}`; | ||
} | ||
else if (typeof end === "undefined") { | ||
range = `bytes=${start}-`; | ||
} | ||
try { | ||
const params = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
Range: range, | ||
}; | ||
const command = new client_s3_1.GetObjectCommand(params); | ||
const response = yield this._client.send(command); | ||
return { value: response.Body, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async _getFileAsStream(bucketName, fileName, options) { | ||
const { start, end } = options; | ||
let range = `bytes=${start}-${end}`; | ||
if (typeof start === "undefined" && typeof end === "undefined") { | ||
range = undefined; | ||
} | ||
else if (typeof start === "undefined") { | ||
range = `bytes=0-${end}`; | ||
} | ||
else if (typeof end === "undefined") { | ||
range = `bytes=${start}-`; | ||
} | ||
try { | ||
const params = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
Range: range, | ||
}; | ||
const command = new client_s3_1.GetObjectCommand(params); | ||
const response = await this._client.send(command); | ||
return { value: response.Body, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_removeFile(bucketName, fileName, allVersions) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.DeleteObjectCommand(input); | ||
const response = yield this._client.send(command); | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async _removeFile(bucketName, fileName, allVersions) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.DeleteObjectCommand(input); | ||
const response = await this._client.send(command); | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_clearBucket(bucketName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
let objects; | ||
// first try to remove the versioned files | ||
const { value, error } = yield this.getFileVersions(bucketName); | ||
if (error === "no versions" || error === "ListObjectVersions not implemented") { | ||
// if that fails remove non-versioned files | ||
const { value, error } = yield this.getFiles(bucketName); | ||
if (error === "no contents") { | ||
return { value: null, error: "Could not remove files" }; | ||
} | ||
else if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
else if (typeof value !== "undefined") { | ||
objects = value.map((value) => ({ Key: value.Key })); | ||
} | ||
async _clearBucket(bucketName) { | ||
let objects; | ||
// first try to remove the versioned files | ||
const { value, error } = await this.getFileVersions(bucketName); | ||
if (error === "no versions" || error === "ListObjectVersions not implemented") { | ||
// if that fails remove non-versioned files | ||
const { value, error } = await this.getFiles(bucketName); | ||
if (error === "no contents") { | ||
return { value: null, error: "Could not remove files" }; | ||
} | ||
@@ -185,156 +163,159 @@ else if (error !== null) { | ||
else if (typeof value !== "undefined") { | ||
objects = value.map((value) => ({ | ||
Key: value.Key, | ||
VersionId: value.VersionId, | ||
})); | ||
objects = value.map((value) => ({ Key: value.Key })); | ||
} | ||
if (typeof objects !== "undefined") { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Delete: { | ||
Objects: objects, | ||
Quiet: false, | ||
}, | ||
}; | ||
const command = new client_s3_1.DeleteObjectsCommand(input); | ||
yield this._client.send(command); | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
return { value: "ok", error: null }; | ||
}); | ||
} | ||
_deleteBucket(bucketName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
else if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
else if (typeof value !== "undefined") { | ||
objects = value.map((value) => ({ | ||
Key: value.Key, | ||
VersionId: value.VersionId, | ||
})); | ||
} | ||
if (typeof objects !== "undefined") { | ||
try { | ||
yield this.clearBucket(bucketName); | ||
const input = { | ||
Bucket: bucketName, | ||
Delete: { | ||
Objects: objects, | ||
Quiet: false, | ||
}, | ||
}; | ||
const command = new client_s3_1.DeleteBucketCommand(input); | ||
const response = yield this._client.send(command); | ||
// console.log(response); | ||
const command = new client_s3_1.DeleteObjectsCommand(input); | ||
await this._client.send(command); | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
// error message Cubbit | ||
if (e.message === "NoSuchBucket") { | ||
return { value: null, error: `The specified bucket does not exist: ${bucketName}` }; | ||
} | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
} | ||
return { value: "ok", error: null }; | ||
} | ||
_addFile(params) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
let fileData; | ||
if (typeof params.origPath !== "undefined") { | ||
const f = params.origPath; | ||
if (!fs_1.default.existsSync(f)) { | ||
return { | ||
value: null, | ||
error: `File with given path: ${f}, was not found`, | ||
}; | ||
} | ||
fileData = fs_1.default.createReadStream(f); | ||
async _deleteBucket(bucketName) { | ||
try { | ||
await this.clearBucket(bucketName); | ||
const input = { | ||
Bucket: bucketName, | ||
}; | ||
const command = new client_s3_1.DeleteBucketCommand(input); | ||
const response = await this._client.send(command); | ||
// console.log(response); | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
// error message Cubbit | ||
if (e.message === "NoSuchBucket") { | ||
return { value: null, error: `The specified bucket does not exist: ${bucketName}` }; | ||
} | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
async _addFile(params) { | ||
try { | ||
let fileData; | ||
if (typeof params.origPath !== "undefined") { | ||
const f = params.origPath; | ||
if (!fs_1.default.existsSync(f)) { | ||
return { | ||
value: null, | ||
error: `File with given path: ${f}, was not found`, | ||
}; | ||
} | ||
else if (typeof params.buffer !== "undefined") { | ||
fileData = params.buffer; | ||
} | ||
else if (typeof params.stream !== "undefined") { | ||
fileData = params.stream; | ||
} | ||
const input = Object.assign({ Bucket: params.bucketName, Key: params.targetPath, Body: fileData }, params.options); | ||
const command = new client_s3_1.PutObjectCommand(input); | ||
const response = yield this._client.send(command); | ||
return this._getFileAsURL(params.bucketName, params.targetPath, params.options); | ||
fileData = fs_1.default.createReadStream(f); | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
else if (typeof params.buffer !== "undefined") { | ||
fileData = params.buffer; | ||
} | ||
}); | ||
else if (typeof params.stream !== "undefined") { | ||
fileData = params.stream; | ||
} | ||
const input = { | ||
Bucket: params.bucketName, | ||
Key: params.targetPath, | ||
Body: fileData, | ||
...params.options, | ||
}; | ||
const command = new client_s3_1.PutObjectCommand(input); | ||
const response = await this._client.send(command); | ||
return this._getFileAsURL(params.bucketName, params.targetPath, params.options); | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_getFileAsURL(bucketName, fileName, options // e.g. { expiresIn: 3600 } | ||
async _getFileAsURL(bucketName, fileName, options // e.g. { expiresIn: 3600 } | ||
) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const url = yield (0, s3_request_presigner_1.getSignedUrl)(this._client, new client_s3_1.GetObjectCommand({ | ||
try { | ||
let url = ""; | ||
if (options.useSignedUrl) { | ||
url = await (0, s3_request_presigner_1.getSignedUrl)(this._client, new client_s3_1.GetObjectCommand({ | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}), options); | ||
return { value: url, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
else { | ||
url = `https://${bucketName}.s3.${this.config.region}.amazonaws.com/${fileName}`; | ||
} | ||
}); | ||
return { value: url, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_listFiles(bucketName, numFiles) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const { value, error } = yield this.getFiles(bucketName, numFiles); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
if (typeof value === "undefined") { | ||
return { value: [], error: null }; | ||
} | ||
return { value: value.map((o) => [o.Key, o.Size]), error: null }; | ||
async _listFiles(bucketName, numFiles) { | ||
try { | ||
const { value, error } = await this.getFiles(bucketName, numFiles); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
if (typeof value === "undefined") { | ||
return { value: [], error: null }; | ||
} | ||
}); | ||
return { value: value.map((o) => [o.Key, o.Size]), error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_sizeOf(bucketName, fileName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.HeadObjectCommand(input); | ||
const response = yield this._client.send(command); | ||
return { value: response.ContentLength, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async _sizeOf(bucketName, fileName) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.HeadObjectCommand(input); | ||
const response = await this._client.send(command); | ||
return { value: response.ContentLength, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
_bucketExists(bucketName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
}; | ||
const command = new client_s3_1.HeadBucketCommand(input); | ||
yield this._client.send(command); | ||
return { value: true, error: null }; | ||
} | ||
catch (e) { | ||
return { value: false, error: null }; | ||
} | ||
}); | ||
async _bucketExists(bucketName) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
}; | ||
const command = new client_s3_1.HeadBucketCommand(input); | ||
await this._client.send(command); | ||
return { value: true, error: null }; | ||
} | ||
catch (e) { | ||
return { value: false, error: null }; | ||
} | ||
} | ||
_fileExists(bucketName, fileName) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.HeadObjectCommand(input); | ||
yield this._client.send(command); | ||
return { value: true, error: null }; | ||
} | ||
catch (e) { | ||
return { value: false, error: null }; | ||
} | ||
}); | ||
async _fileExists(bucketName, fileName) { | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
Key: fileName, | ||
}; | ||
const command = new client_s3_1.HeadObjectCommand(input); | ||
await this._client.send(command); | ||
return { value: true, error: null }; | ||
} | ||
catch (e) { | ||
return { value: false, error: null }; | ||
} | ||
} | ||
@@ -354,52 +335,50 @@ // public | ||
} | ||
listBuckets() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
var _a; | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
try { | ||
const input = {}; | ||
const command = new client_s3_1.ListBucketsCommand(input); | ||
const response = yield this._client.send(command); | ||
const bucketNames = (_a = response.Buckets) === null || _a === void 0 ? void 0 : _a.map((b) => b === null || b === void 0 ? void 0 : b.Name); | ||
return { value: bucketNames, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
async listBuckets() { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
try { | ||
const input = {}; | ||
const command = new client_s3_1.ListBucketsCommand(input); | ||
const response = await this._client.send(command); | ||
const bucketNames = response.Buckets?.map((b) => b?.Name); | ||
return { value: bucketNames, error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
createBucket(bucketName_1) { | ||
return __awaiter(this, arguments, void 0, function* (bucketName, options = {}) { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
async createBucket(bucketName, options = {}) { | ||
if (this.configError !== null) { | ||
return { value: null, error: this.configError }; | ||
} | ||
const error = (0, util_1.validateName)(bucketName); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
try { | ||
const input = { | ||
Bucket: bucketName, | ||
...options, | ||
}; | ||
const command = new client_s3_1.CreateBucketCommand(input); | ||
const response = await this._client.send(command); | ||
// console.log(response.Location, response.Location.indexOf(bucketName)); | ||
/* | ||
console.log(response); | ||
// not sure if this is necessary | ||
if (response.$metadata.httpStatusCode === 200) { | ||
return { value: "ok", error: null }; | ||
} else { | ||
return { | ||
value: null, | ||
error: `Error http status code ${response.$metadata.httpStatusCode}`, | ||
}; | ||
} | ||
const error = (0, util_1.validateName)(bucketName); | ||
if (error !== null) { | ||
return { value: null, error }; | ||
} | ||
try { | ||
const input = Object.assign({ Bucket: bucketName }, options); | ||
const command = new client_s3_1.CreateBucketCommand(input); | ||
const response = yield this._client.send(command); | ||
// console.log(response.Location, response.Location.indexOf(bucketName)); | ||
/* | ||
console.log(response); | ||
// not sure if this is necessary | ||
if (response.$metadata.httpStatusCode === 200) { | ||
return { value: "ok", error: null }; | ||
} else { | ||
return { | ||
value: null, | ||
error: `Error http status code ${response.$metadata.httpStatusCode}`, | ||
}; | ||
} | ||
*/ | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
}); | ||
*/ | ||
return { value: "ok", error: null }; | ||
} | ||
catch (e) { | ||
return { value: null, error: e.message }; | ||
} | ||
} | ||
@@ -406,0 +385,0 @@ } |
{ | ||
"name": "@tweedegolf/sab-adapter-amazon-s3", | ||
"version": "1.0.15", | ||
"version": "1.0.17", | ||
"description": "Provides an abstraction layer for interacting with Amazon S3 and S3 compatible cloud services.", | ||
@@ -10,4 +10,4 @@ "main": "dist/index/AdapterAmazonS3.js", | ||
"dependencies": { | ||
"@aws-sdk/client-s3": "^3.550.0", | ||
"@aws-sdk/s3-request-presigner": "^3.550.0" | ||
"@aws-sdk/client-s3": "^3.582.0", | ||
"@aws-sdk/s3-request-presigner": "^3.582.0" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": {}, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
86693
1387