react-s3-typescript
Version:
A npm package to upload your files into AWS S3 Bucket directly using react
159 lines • 6.84 kB
JavaScript
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());
});
};
import shortId from 'short-uuid';
import { throwUploadError } from './ErrorThrower';
import GetUrl from './Url';
import { S3Client, PutObjectCommand, DeleteObjectCommand } from '@aws-sdk/client-s3';
import { detectFileType } from 'type-teller';
import { Upload } from '@aws-sdk/lib-storage';
class ReactS3Client {
constructor(config) {
this.config = config;
}
uploadFile(file, newFileName) {
return __awaiter(this, void 0, void 0, function* () {
throwUploadError(this.config, file);
let fileExtension = '';
let mimType = '';
if (file instanceof File) {
if (file.name) {
fileExtension = file.name.split('.').pop() || '';
}
if (!fileExtension && file.type != null) {
fileExtension = file.type.split('/').pop() || '';
}
}
if (Buffer.isBuffer(file)) {
const fileType = yield detectFileType(file);
fileExtension = (fileType === null || fileType === void 0 ? void 0 : fileType.ext) || '';
mimType = (fileType === null || fileType === void 0 ? void 0 : fileType.mime) || '';
}
const fileName = `${newFileName || shortId.generate()}${fileExtension && '.' + fileExtension}`;
const dirName = (this.config.dirName ? this.config.dirName + '/' : '').replace(/([^:]\/)\/+/g, '$1');
const key = `${dirName}${fileName}`;
const url = GetUrl(this.config);
const client = new S3Client({
region: this.config.region,
credentials: {
accessKeyId: this.config.accessKeyId,
secretAccessKey: this.config.secretAccessKey,
}
});
const params = {
Bucket: this.config.bucketName,
Key: key,
Body: file,
Metadata: {
uuid: "14365123651274",
tag: "",
},
ContentType: file instanceof File ? file.type : mimType
};
const command = new PutObjectCommand(params);
const data = yield client.send(command);
if (data["$metadata"]["httpStatusCode"] !== 200)
return Promise.reject(data);
return Promise.resolve({
bucket: this.config.bucketName,
key,
location: `${url}/${key}`,
status: 0,
});
});
}
uploadWithProgress(file, progressCallback, newFileName) {
return __awaiter(this, void 0, void 0, function* () {
throwUploadError(this.config, file);
let fileExtension = '';
let mimType = '';
if (file instanceof File) {
if (file.name) {
fileExtension = file.name.split('.').pop() || '';
}
if (!fileExtension && file.type != null) {
fileExtension = file.type.split('/').pop() || '';
}
}
if (Buffer.isBuffer(file)) {
const fileType = yield detectFileType(file);
fileExtension = (fileType === null || fileType === void 0 ? void 0 : fileType.ext) || '';
mimType = (fileType === null || fileType === void 0 ? void 0 : fileType.mime) || '';
}
const fileName = `${newFileName || shortId.generate()}${fileExtension && '.' + fileExtension}`;
const dirName = (this.config.dirName ? this.config.dirName + '/' : '').replace(/([^:]\/)\/+/g, '$1');
const key = `${dirName}${fileName}`;
const url = GetUrl(this.config);
const client = new S3Client({
region: this.config.region,
credentials: {
accessKeyId: this.config.accessKeyId,
secretAccessKey: this.config.secretAccessKey,
}
});
const params = {
Bucket: this.config.bucketName,
Key: key,
Body: file,
Metadata: {
uuid: "14365123651274",
tag: "",
},
ContentType: file instanceof File ? file.type : mimType
};
const upload = new Upload({
client: client,
params: params
});
upload.on("httpUploadProgress", progress => {
progressCallback(Number(progress.loaded) / Number(progress.total), progress);
});
const data = yield upload.done();
if (data["$metadata"]["httpStatusCode"] !== 200)
return Promise.reject(data);
return Promise.resolve({
bucket: this.config.bucketName,
key: data.Key || key,
location: data.Location || url,
status: 0,
});
});
}
deleteFile(key) {
return __awaiter(this, void 0, void 0, function* () {
const client = new S3Client({
region: this.config.region,
credentials: {
accessKeyId: this.config.accessKeyId,
secretAccessKey: this.config.secretAccessKey,
}
});
const params = {
Bucket: this.config.bucketName,
Key: key,
};
const deleteCommand = new DeleteObjectCommand(params);
return new Promise((resolve, reject) => {
client.send(deleteCommand, (err) => {
if (err) {
reject(err);
}
else {
resolve({
message: "File Deleted Successfully!",
key: key
});
}
});
});
});
}
}
export default ReactS3Client;
//# sourceMappingURL=react-aws-s3.js.map