Socket
Socket
Sign inDemoInstall

simple-s3-file-upload

Package Overview
Dependencies
165
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 4.1.2 to 5.1.0

dist/index.cjs

12

CHANGELOG.md
# simple-s3-file-upload
## 5.1.0
### Minor Changes
- Updated tsup
## 5.0.0
### Major Changes
- Added presignedUrl uploader
## 4.1.2

@@ -4,0 +16,0 @@

14

dist/index.d.ts

@@ -44,3 +44,15 @@ import * as _aws_sdk_client_s3 from '@aws-sdk/client-s3';

}>;
declare const uploadFileUsingPresignedUrl: ({ accessKeyId, secretAccessKey, region, bucket, path, targetFile, }: {
accessKeyId: string;
secretAccessKey: string;
region: string;
bucket: string;
path: string;
targetFile: HTMLFormElement;
}) => Promise<{
message: string;
response: Response;
url: string;
}>;
export { uploadFile, uploadImage, uploadVideo };
export { uploadFile, uploadFileUsingPresignedUrl, uploadImage, uploadVideo };

@@ -24,2 +24,3 @@ "use strict";

uploadFile: () => uploadFile,
uploadFileUsingPresignedUrl: () => uploadFileUsingPresignedUrl,
uploadImage: () => uploadImage,

@@ -30,2 +31,3 @@ uploadVideo: () => uploadVideo

var import_client_s3 = require("@aws-sdk/client-s3");
var import_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
var import_buffer = require("buffer");

@@ -149,7 +151,53 @@ var getImageBuffer = (base64) => {

};
var uploadFileUsingPresignedUrl = async ({
accessKeyId,
secretAccessKey,
region,
bucket,
path,
targetFile
}) => {
try {
const formData = new FormData(targetFile);
const file = formData.get("file");
if (!file) {
throw new Error("File not found");
}
if (!(file instanceof import_buffer.File)) {
throw new Error("File not found or invalid type");
}
const fileType = encodeURIComponent(file.type);
const generatedPath = `${path}${(/* @__PURE__ */ new Date()).toISOString()}.${fileType}`;
const client = s3Client({ accessKeyId, secretAccessKey, region });
const command = new import_client_s3.PutObjectCommand({
Bucket: bucket,
Key: generatedPath
});
const preSignedUrl = await (0, import_s3_request_presigner.getSignedUrl)(client, command, {
expiresIn: 3600
});
const response = await fetch(preSignedUrl, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: file
});
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return {
message: "File uploaded successfully",
response,
url: `https://${bucket}.s3.${region}.amazonaws.com/${generatedPath}`
};
} catch (error) {
console.log(error);
throw new Error(`Error uploading file: ${error.message}`);
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
uploadFile,
uploadFileUsingPresignedUrl,
uploadImage,
uploadVideo
});

14

package.json
{
"name": "simple-s3-file-upload",
"version": "4.1.2",
"version": "5.1.0",
"description": "Simple S3 Image Upload is a Node.js package that simplifies the process of uploading images to Amazon S3 (Simple Storage Service). It uses the AWS SDK for JavaScript and the Buffer module to handle image data. This package allows you to quickly and easily upload images to your S3 bucket",

@@ -35,14 +35,12 @@ "main": "dist/index.js",

"@changesets/cli": "^2.26.2",
"@types/node": "^20.8.9",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
"vitest": "^0.34.6"
"@types/node": "^20.9.4",
"tsup": "^8.0.1",
"typescript": "^5.3.2"
},
"dependencies": {
"@aws-sdk/client-s3": "^3.438.0",
"@aws-sdk/client-s3": "^3.456.0",
"@aws-sdk/s3-request-presigner": "^3.456.0",
"buffer": "^6.0.3"
},
"scripts": {
"dev": "vitest",
"test": "vitest run",
"build": "tsup src/index.ts --format cjs,esm --dts",

@@ -49,0 +47,0 @@ "lint": "tsc",

@@ -198,3 +198,3 @@ # Simple S3 Image Upload

```javascript
````javascript
import { uploadFile } from 'your-module-path';

@@ -226,4 +226,65 @@

# `uploadFileUsingPresignedUrl` Function Documentation
The `uploadFileUsingPresignedUrl` function is designed to facilitate the upload of files to an Amazon S3 bucket using a presigned URL. This function is asynchronous and takes an object as an argument with the following properties:
## Input Parameters
- `accessKeyId` (type: `string`, required): AWS access key ID for S3 authentication.
- `secretAccessKey` (type: `string`, required): AWS secret access key for S3 authentication.
- `region` (type: `string`, required): AWS region where the S3 bucket is located.
- `bucket` (type: `string`, required): Name of the S3 bucket where the file will be uploaded.
- `path` (type: `string`, required): Path within the S3 bucket where the file will be stored.
- `targetFile` (type: `HTMLFormElement`, required): The HTML form element containing the file to be uploaded.
## Output
The function returns a Promise that resolves to an object with the following properties:
- `message` (type: `string`): A success message indicating that the file was uploaded successfully.
- `response` (type: `Response`): The response object from the fetch operation.
- `url` (type: `string`): The public URL of the uploaded file.
## Error Handling
If the `targetFile` parameter is not provided or does not contain a valid file, the function throws an `Error` with the message "File not found or invalid type."
If an error occurs during the S3 upload process or the fetch operation, the function catches the error, logs it to the console, and throws an `Error` with a detailed error message.
## Dependencies
This function relies on an external `s3Client` function, assumed to be correctly implemented elsewhere in your codebase. Additionally, it uses the `getSignedUrl` function to generate a presigned URL for S3 uploads.
Please make sure to handle the AWS SDK and any other dependencies appropriately in your project.
## Example Usage
```javascript
import { uploadFileUsingPresignedUrl } from 'your-module-path';
const accessKeyId = 'your-access-key-id';
const secretAccessKey = 'your-secret-access-key';
const region = 'your-aws-region';
const bucket = 'your-s3-bucket';
const path = 'your-s3-path';
const targetFile = /* provide the HTML form element containing the file */;
uploadFileUsingPresignedUrl({
accessKeyId,
secretAccessKey,
region,
bucket,
path,
targetFile,
})
.then((result) => {
console.log('File uploaded successfully:', result.message);
console.log('Public URL:', result.url);
})
.catch((error) => {
console.error('Error during file upload:', error.message);
});
## Dependencies
This function relies on an external `s3Client` function, assumed to be correctly implemented elsewhere in your codebase.

@@ -244,2 +305,2 @@

**Note**: Ensure that you have the required AWS permissions and have configured your S3 bucket to allow the intended actions for successful image uploads.
```
````

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc