aws-s3-upload-ash
Advanced tools
Comparing version 1.0.13 to 1.1.0
@@ -1,2 +0,2 @@ | ||
var e,t=(e=require("crypto-js"))&&"object"==typeof e&&"default"in e?e.default:e,r=new Date(+new Date+864e5).toISOString(),n=r.split("-").join("").split(":").join("").split(".").join(""),i=r.split("T")[0].split("-").join(""),o=function(){};o.getPolicy=function(e){return new Buffer(JSON.stringify({expiration:r,conditions:[{acl:"public-read"},{bucket:e.bucketName},["starts-with","$key",e.dirName?e.dirName+"/":""],["starts-with","$Content-Type",""],["starts-with","$x-amz-meta-tag",""],{"x-amz-algorithm":"AWS4-HMAC-SHA256"},{"x-amz-credential":e.accessKeyId+"/"+i+"/"+e.region+"/s3/aws4_request"},{"x-amz-date":n},{"x-amz-meta-uuid":"14365123651274"},{"x-amz-server-side-encryption":"AES256"}]})).toString("base64").replace(/\n|\r/,"")};var a=function(){};a.getSignature=function(e,r,n){var i,o,a,c,s,u;return t.HmacSHA256(n,(i=e.secretAccessKey,o=r,a=e.region,c=t.HmacSHA256(o,"AWS4"+i),s=t.HmacSHA256(a,c),u=t.HmacSHA256("s3",s),t.HmacSHA256("aws4_request",u))).toString(t.enc.Hex)};var c=function(e){this.config=e};c.prototype.uploadFile=function(e,t,r,c){try{var s=this;!function(e,t){if(null===e.bucketName||""===e.bucketName)throw new Error("Your bucketName cannot be empty ");if(null===e.region||""===e.region)throw new Error("Must provide a valide region in order to use your bucket");if(null===e.accessKeyId||""===e.accessKeyId)throw new Error("Must provide accessKeyId");if(null===e.secretAccessKey||""===e.secretAccessKey)throw new Error("Must provide secretAccessKey");if(!t)throw new Error("File cannot be empty")}(s.config,e);var u=new FormData,m=""+t,l=(s.config.dirName?s.config.dirName+"/":"")+m,p=(f=s.config).s3Url&&""!==f.s3Url?f.s3Url:function(e){var t=e.bucketName,r=e.region,n=r.split("-")[0];switch(n){case"cn":return"https://"+t+".s3."+r+".amazonaws.com."+n;default:return"https://"+t+".s3-"+r+".amazonaws.com"}}(f),d=null==c||""===c?"public-read":c;return console.log("aclFinal",d),u.append("key",l),u.append("acl",d),u.append("Content-Type",r),u.append("x-amz-meta-uuid","14365123651274"),u.append("x-amz-server-side-encryption","AES256"),u.append("X-Amz-Credential",s.config.accessKeyId+"/"+i+"/"+s.config.region+"/s3/aws4_request"),u.append("X-Amz-Algorithm","AWS4-HMAC-SHA256"),u.append("X-Amz-Date",n),u.append("x-amz-meta-tag",""),u.append("Policy",o.getPolicy(s.config)),u.append("X-Amz-Signature",a.getSignature(s.config,i,o.getPolicy(s.config))),u.append("file",e),Promise.resolve(fetch(p,{method:"post",body:u})).then(function(e){return e.ok?Promise.resolve({bucket:s.config.bucketName,key:(s.config.dirName?s.config.dirName+"/":"")+m,location:p+"/"+(s.config.dirName?s.config.dirName+"/":"")+m,status:e.status}):Promise.reject(e)})}catch(e){return Promise.reject(e)}var f},c.prototype.deleteFile=function(e){try{return Promise.resolve(fetch("https://"+this.config.bucketName+".s3"+(this.config.region?"-"+this.config.region:"")+".amazonaws.com/"+(this.config.dirName?this.config.dirName+"/":"")+e,{method:"delete"})).then(function(t){return t.ok?Promise.resolve({ok:t.ok,status:t.status,message:"File Deleted",fileName:e}):Promise.reject(t)})}catch(e){return Promise.reject(e)}},module.exports=c; | ||
var e,t=(e=require("crypto-js"))&&"object"==typeof e&&"default"in e?e.default:e;"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var r=new Date(+new Date+864e5).toISOString(),o=r.split("-").join("").split(":").join("").split(".").join(""),n=r.split("T")[0].split("-").join(""),i=function(){};i.getPolicy=function(e){return new Buffer(JSON.stringify({expiration:r,conditions:[{acl:"public-read"},{bucket:e.bucketName},["starts-with","$key",e.dirName?e.dirName+"/":""],["starts-with","$Content-Type",""],["starts-with","$x-amz-meta-tag",""],{"x-amz-algorithm":"AWS4-HMAC-SHA256"},{"x-amz-credential":e.accessKeyId+"/"+n+"/"+e.region+"/s3/aws4_request"},{"x-amz-date":o},{"x-amz-meta-uuid":"14365123651274"},{"x-amz-server-side-encryption":"AES256"}]})).toString("base64").replace(/\n|\r/,"")};var a=function(){};a.getSignature=function(e,r,o){var n,i,a,c,s,m;return t.HmacSHA256(o,(n=e.secretAccessKey,i=r,a=e.region,c=t.HmacSHA256(i,"AWS4"+n),s=t.HmacSHA256(a,c),m=t.HmacSHA256("s3",s),t.HmacSHA256("aws4_request",m))).toString(t.enc.Hex)};var c=function(e){this.config={},e&&(this.config=e)};c.prototype.uploadFile=function(e,t,r,c,s){try{var m=this;if(!e)throw new Error("File cannot be empty");if(r||function(e,t){if(null===e.bucketName||""===e.bucketName)throw new Error("Your bucketName cannot be empty ");if(null===e.region||""===e.region)throw new Error("Must provide a valide region in order to use your bucket");if(null===e.accessKeyId||""===e.accessKeyId)throw new Error("Must provide accessKeyId");if(null===e.secretAccessKey||""===e.secretAccessKey)throw new Error("Must provide secretAccessKey");if(!t)throw new Error("File cannot be empty")}(m.config,e),r)return Promise.resolve(function(e,t){try{var r=e()}catch(e){return t(e)}return r&&r.then?r.then(void 0,t):r}(function(){return Promise.resolve(fetch(r,{method:"PUT",headers:{"Content-Type":t},body:e})).then(function(){return Promise.resolve({status:200,body:"Upload complete"})})},function(e){return Promise.resolve({status:400,body:e})}));var u=new FormData,l=""+c,d=(m.config.dirName?m.config.dirName+"/":"")+l,f=(y=m.config).s3Url&&""!==y.s3Url?y.s3Url:function(e){var t=e.bucketName,r=e.region,o=r.split("-")[0];switch(o){case"cn":return"https://"+t+".s3."+r+".amazonaws.com."+o;default:return"https://"+t+".s3-"+r+".amazonaws.com"}}(y),p=null==s||""===s?"public-read":s;return u.append("key",d),u.append("acl",p),u.append("Content-Type",t),u.append("x-amz-meta-uuid","14365123651274"),u.append("x-amz-server-side-encryption","AES256"),u.append("X-Amz-Credential",m.config.accessKeyId+"/"+n+"/"+m.config.region+"/s3/aws4_request"),u.append("X-Amz-Algorithm","AWS4-HMAC-SHA256"),u.append("X-Amz-Date",o),u.append("x-amz-meta-tag",""),u.append("Policy",i.getPolicy(m.config)),u.append("X-Amz-Signature",a.getSignature(m.config,n,i.getPolicy(m.config))),u.append("file",e),Promise.resolve(fetch(f,{method:"post",body:u})).then(function(e){return e.ok?Promise.resolve({bucket:m.config.bucketName,key:(m.config.dirName?m.config.dirName+"/":"")+l,location:f+"/"+(m.config.dirName?m.config.dirName+"/":"")+l,status:e.status}):Promise.reject(e)})}catch(e){return Promise.reject(e)}var y},c.prototype.deleteFile=function(e){try{var t=this;return Promise.resolve(fetch("https://"+t.config.bucketName+".s3"+(t.config.region?"-"+t.config.region:"")+".amazonaws.com/"+(t.config.dirName?t.config.dirName+"/":"")+e,{method:"delete"})).then(function(r){return r.ok?Promise.resolve({ok:r.ok,status:r.status,message:"File Deleted",fileName:t.config.dirName?t.config.dirName+"/"+e:e}):Promise.reject(r)})}catch(e){return Promise.reject(e)}},module.exports=c; | ||
//# sourceMappingURL=aws-s3-upload-ash.js.map |
{ | ||
"name": "aws-s3-upload-ash", | ||
"version": "1.0.13", | ||
"description": "Open Source Module to Upload your Media and files into AWS S3 Bucket directly", | ||
"version": "1.1.0", | ||
"description": "Open Source Module to Upload your Media and files into AWS S3 Bucket directly from Front-end", | ||
"author": "Ismael Ash", | ||
"email": "contato@ismaelnascimento.com", | ||
"url": "https://ismaelnascimento.com", | ||
"funding": "http://bit.ly/doeismaelnascimento", | ||
"main": "dist/aws-s3-upload-ash.js", | ||
@@ -29,3 +32,3 @@ "module": "dist/aws-s3-upload-ash.m.js", | ||
"vanillajs", | ||
"vue-aws3", | ||
"vue-awss3", | ||
"vuejs", | ||
@@ -39,3 +42,3 @@ "typescript", | ||
"aws-s3-upload-ash", | ||
"aws-s3-upload-ash" | ||
"presignedurl" | ||
], | ||
@@ -54,8 +57,8 @@ "license": "MIT", | ||
"type": "git", | ||
"url": "git+https://github.com/ismaelash/aws-s3-react.git" | ||
"url": "git+https://github.com/ismaelash/aws-s3-upload-ash.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ismaelash/aws-s3-react/issues" | ||
"url": "https://github.com/ismaelash/aws-s3-upload-ash/issues" | ||
}, | ||
"homepage": "https://github.com/ismaelash/aws-s3-react#readme" | ||
"homepage": "https://github.com/ismaelash/aws-s3-upload-ash#readme" | ||
} |
228
README.md
@@ -1,16 +0,18 @@ | ||
# aws-s3-reactjs | ||
Open Source Module to Upload your Media and files into AWS S3 Bucket directly from Front-end React. | ||
# aws-s3-upload-ash | ||
Open Source Module to Upload your Media and files into AWS S3 Bucket directly from Front-end. | ||
<br> | ||
AWSS3UploadAsh - A Javascript Library for AWS S3 File Upload | ||
## Video | ||
How to use = https://youtu.be/1U9Ohj3T4gI | ||
# Donate | ||
**http://bit.ly/doeismaelnascimento** | ||
### https://www.npmjs.com/package/aws-s3-reactjs | ||
# S3Client AWS-S3 | ||
## How to use(youtube) | ||
* How to use with React = | ||
* How to use with Angular = | ||
# How get | ||
S3Client - A Javascript Library for AWS S3 File Upload | ||
Using NPM | ||
``` | ||
npm install --save aws-s3-reactjs | ||
npm install aws-s3-upload-ash | ||
``` | ||
@@ -20,3 +22,3 @@ Using Yarn | ||
``` | ||
yarn add aws-s3-reactjs | ||
yarn add aws-s3-upload-ash | ||
``` | ||
@@ -26,26 +28,31 @@ | ||
# Examples Uploading An Image | ||
# Examples Uploading a file | ||
## ***Uploading to S3*** | ||
## ***Uploading to S3 with bucket public*** | ||
```js | ||
import S3 from 'aws-s3-reactjs'; | ||
import AWSS3UploadAsh from 'aws-s3-upload-ash'; | ||
const config = { | ||
bucketName: 'myBucket', | ||
dirName: 'media', /* optional */ | ||
region: 'eu-west-1', | ||
accessKeyId: 'JAJHAFJFHJDFJSDHFSDHFJKDSF', | ||
secretAccessKey: 'jhsdf99845fd98qwed42ebdyeqwd-3r98f373f=qwrq3rfr3rf', | ||
s3Url: 'https:/your-custom-s3-url.com/' | ||
bucketName: 'bucketName', | ||
dirName: 'media', /* optional - when use: e.g BUCKET_ROOT/dirName/fileName.extesion */ | ||
region: 'us-east-1', | ||
accessKeyId: process.env.accessKeyId, | ||
secretAccessKey: process.env.secretAccessKey, | ||
s3Url: 'https://bucketName.s3.amazonaws.com/' | ||
} | ||
const ReactS3Client = new S3(config); | ||
/* Notice that if you don't provide a dirName, the file will be automatically uploaded to the root of your bucket */ | ||
// if you bucket is public, you need of config | ||
const S3CustomClient = new AWSS3UploadAsh(config); | ||
const newFileName = 'fileName.extesion'; | ||
// https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html | ||
// acl default: public-read | ||
ReactS3Client | ||
.uploadFile(file, newFileName, "contentType", "acl-optional") | ||
const newFileNameWithExtesion = 'myPdf.pdf'; | ||
//file: File - required | e.g input html type file | ||
//contentType: string | required e.g application/pdf | ||
//presignedURL: string | optional | ||
//newFileName: string | optional e.g myImage.png | ||
//acl: string | optional default public-read | ||
// if you use presignedURL, not need newFileName and acl parameters, can be null | ||
S3CustomClient | ||
.uploadFile(file, "application/pdf", null, newFileNameWithExtesion, null) | ||
.then(data => console.log(data)) | ||
@@ -55,7 +62,39 @@ .catch(err => console.error(err)) | ||
/** | ||
* | ||
* { | ||
* bucket: "bucketName", | ||
* key: "media/myPdf.pdf", | ||
* location: "https://bucketName.s3.amazonaws.com/media/myPdf.pdf", | ||
* status: 204 | ||
* } | ||
* | ||
*/ | ||
}); | ||
``` | ||
## ***Uploading to S3 with presignedURL*** | ||
```js | ||
import AWSS3UploadAsh from 'aws-s3-upload-ash'; | ||
// if you use presignedURL, dont need config on AWSS3UploadAsh constructor | ||
const S3CustomClient = new AWSS3UploadAsh(); | ||
//file: File - required | e.g input html type file | ||
//contentType: string | required e.g application/pdf | ||
//presignedURL: string | optional | ||
//newFileName: string | optional e.g myImage.png | ||
//acl: string | optional default public-read | ||
// if you use presignedURL, not need newFileName and acl parameters, can be null | ||
S3CustomClient | ||
.uploadFile(file, "application/png", "presignedlURL", null, null) | ||
.then(data => console.log(data)) | ||
.catch(err => console.error(err)) | ||
/** | ||
* Response se you use presignedURL parameter | ||
* { | ||
* Response: { | ||
* bucket: "myBucket", | ||
* key: "image/test-image.jpg", | ||
* location: "https://myBucket.s3.amazonaws.com/media/test-file.jpg" | ||
* status: 200, | ||
* body: "Upload complete" | ||
* } | ||
@@ -67,29 +106,67 @@ * } | ||
## ***Deleting an existing file in your bucket*** | ||
## ***Uploading to S3 with bucket public and without directory*** | ||
```js | ||
import AWSS3UploadAsh from 'aws-s3-upload-ash'; | ||
const config = { | ||
bucketName: 'bucketName', | ||
region: 'us-east-1', | ||
accessKeyId: process.env.accessKeyId, | ||
secretAccessKey: process.env.secretAccessKey, | ||
s3Url: 'https://bucketName.s3.amazonaws.com/' | ||
} | ||
// if you bucket is public, you need of config | ||
const S3CustomClient = new AWSS3UploadAsh(config); | ||
const newFileNameWithExtesion = 'myVideo.mp4'; | ||
//file: File - required | e.g input html type file | ||
//contentType: string | required e.g application/pdf | ||
//presignedURL: string | optional | ||
//newFileName: string | optional e.g myImage.png | ||
//acl: string | optional default public-read | ||
// if you use presignedURL, not need newFileName and acl parameters, can be null | ||
S3CustomClient | ||
.uploadFile(file, "video/mp4", null, newFileNameWithExtesion, null) | ||
.then(data => console.log(data)) | ||
.catch(err => console.error(err)) | ||
/** | ||
* | ||
* { | ||
* bucket: "bucketName", | ||
* key: "myVideo.mp4", | ||
* location: "https://bucketName.s3.amazonaws.com/myVideo.mp4", | ||
* status: 204 | ||
* } | ||
* | ||
*/ | ||
}); | ||
``` | ||
## ***Deleting an existing file into directory in your bucket public*** | ||
In this case the file that we want to delete is in the folder 'photos' | ||
```js | ||
import S3 from 'aws-s3-reactjs'; | ||
import AWSS3UploadAsh from 'aws-s3-upload-ash'; | ||
const config = { | ||
bucketName: 'myBucket', | ||
dirName: 'media', /* optional */ | ||
region: 'eu-west-1', | ||
accessKeyId: 'JAJHAFJFHJDFJSDHFSDHFJKDSF', | ||
secretAccessKey: 'jhsdf99845fd98qwed42ebdyeqwd-3r98f373f=qwrq3rfr3rf', | ||
s3Url: 'https:/your-custom-s3-url.com/' | ||
bucketName: 'bucketName', | ||
dirName: 'media', | ||
region: 'us-east-1', | ||
accessKeyId: process.env.accessKeyId, | ||
secretAccessKey: process.env.secretAccessKey, | ||
s3Url: 'https://bucketName.s3.amazonaws.com/' | ||
} | ||
const ReactS3Client = new S3(config); | ||
const S3CustomClient = new AWSS3UploadAsh(config); | ||
const filename = 'hello-world.docx'; | ||
const newFileNameWithExtesion = 'fileName.extesion'; | ||
/* If the file that you want to delete it's in your bucket's root folder, don't provide any dirName in the config object */ | ||
//In this case the file that we want to delete is in the folder 'photos' that we referred in the config object as the dirName | ||
ReactS3Client | ||
.deleteFile(filename) | ||
S3CustomClient | ||
.deleteFile(newFileNameWithExtesion) | ||
.then(response => console.log(response)) | ||
@@ -102,5 +179,5 @@ .catch(err => console.error(err)) | ||
* ok: true, | ||
status: 204, | ||
message: 'File deleted', | ||
fileName: 'hello-world.docx' | ||
* status: 204, | ||
* message: 'File deleted', | ||
* fileName: 'media/fileName.extesion'; | ||
* } | ||
@@ -112,20 +189,51 @@ * } | ||
Defaults your bucket to `public-read` : http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html | ||
## ***Deleting an existing file without directory in your bucket public*** | ||
`config` | ||
* `bucketName` **required** - Your S3 bucket | ||
* `dirName` **optional** - Your S3 folderName/dirName | ||
* `region` **required** - Your S3 bucket's region | ||
* `accessKeyId` **required** - Your S3 `AccessKeyId` | ||
* `secretAccessKey` **required** - Your S3 `SecretAccessKey` | ||
* `s3Url` **required** - Your S3 URL | ||
```js | ||
import AWSS3UploadAsh from 'aws-s3-upload-ash'; | ||
## License | ||
const config = { | ||
bucketName: 'bucketName', | ||
region: 'us-east-1', | ||
accessKeyId: process.env.accessKeyId, | ||
secretAccessKey: process.env.secretAccessKey, | ||
s3Url: 'https://bucketName.s3.amazonaws.com/' | ||
} | ||
## ***S3 Bucket Policy*** | ||
const S3CustomClient = new AWSS3UploadAsh(config); | ||
Doc: http://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html | ||
const newFileNameWithExtesion = 'fileName.extesion'; | ||
S3CustomClient | ||
.deleteFile(newFileNameWithExtesion) | ||
.then(response => console.log(response)) | ||
.catch(err => console.error(err)) | ||
MIT | ||
/** | ||
* { | ||
* Response: { | ||
* ok: true, | ||
* status: 204, | ||
* message: 'File deleted', | ||
* fileName: 'fileName.extesion'; | ||
* } | ||
* } | ||
*/ | ||
}); | ||
``` | ||
# Important | ||
1. If you bucket is public use only this parameters: file, contentType, newFileNameWithExtesion see the example above | ||
2. If you bucket is private(with Objects can be public) use presignedURL parameter (recommended) | ||
## ***AWS S3 Links*** | ||
- S3 Bucket Policies: https://docs.aws.amazon.com/AmazonS3/latest/dev/example-bucket-policies.html | ||
- ACL: https://docs.aws.amazon.com/AmazonS3/latest/userguide/acl-overview.html | ||
- Presigned URL: https://docs.aws.amazon.com/AmazonS3/latest/userguide/ShareObjectPreSignedURL.html | ||
- Upload Presigned URL: https://docs.aws.amazon.com/AmazonS3/latest/userguide/PresignedUrlUploadObject.html | ||
- Input html: https://www.w3schools.com/tags/tag_input.asp | ||
## License | ||
**MIT** |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
12196
16
0
1
234
3