Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post policy example update #1056

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 97 additions & 28 deletions examples/presigned-postpolicy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,107 @@ var s3Client = new Minio.Client({
useSSL: true // Default is true.
})

// Construct a new postPolicy.
var policy = s3Client.newPostPolicy()
// Set the object name my-objectname.
policy.setKey("my-objectname")
// Set the bucket to my-bucketname.
policy.setBucket("my-bucketname")
const superagent = require('superagent')
const _ = require('lodash')

var expires = new Date
expires.setSeconds(24 * 60 * 60 * 10) //10 days
policy.setExpires(expires)

policy.setContentLengthRange(1024, 1024*1024) // Min upload length is 1KB Max upload size is 1MB
var s3Client = new Minio.Client({
useSSL:false,
endPoint:"localhost",
port:22000,
accessKey:"minio",
secretKey:"minio123"
})

policy.setContentType('text/plain')
// s3Client.traceOn()
function testViaHttpClient () {

policy.setContentDisposition('attachment; filename=text.txt')
// Construct a new postPolicy.
var policy = s3Client.newPostPolicy()
// Set the object name my-objectname.
policy.setKey("infra-policy-new.txt")
// Set the bucket to my-bucketname.
policy.setBucket("post-policy-test")

policy.setUserMetaData({
key: 'value'
})
var expires = new Date()
expires.setSeconds(24 * 60 * 60 * 10) // 10 days
policy.setExpires(expires)

policy.setContentLengthRange(1, 1024 * 1024) // Min upload length is 1KB Max upload size is 1MB

policy.setContentTypeStartsWith('image/jpeg')
policy.setContentDisposition('attachment; filename=äöüex ®©µÄÆÐÕæŒƕƩDž 01000000 0x40 \u0040 amȡȹɆple&0a!-_.*\'()&$@=;:+,?<>.pdf')

policy.setUserMetaData({
"sph-meta": 'sph-value',
"filename-meta": 'My* Docume nt.json'
})

s3Client.presignedPostPolicy(policy, function(e, data) {
if (e) return console.log(e)
var curl = []
curl.push(`curl ${data.postURL}`)
for (var key in data.formData) {
if (data.formData.hasOwnProperty(key)) {
var value = data.formData[key]
curl.push(`-F ${key}=${value}`)

s3Client.presignedPostPolicy(policy, (e, data) => {
if (e) {
return console.log(e)
}
}
// Print curl command to upload files.
curl.push('-F file=@<FILE>')
console.log(curl.join(' '))
})
var req = superagent.post(data.postURL)
_.each(data.formData, (value, key) => req.field(key, value))
req.attach('file', Buffer.from(Buffer.alloc(1, 0)), 'test')
req.end(function (e) {
if (e) {
console.log(e)
}
// s3Client.removeObject(bucketName, objectName, done)
})
req.on('error', e => {
console.log(e)
})
})

}

function generateCurl(){
var policy = s3Client.newPostPolicy()
// Set the object name my-objectname.
policy.setKey("Cust_Mobile_View.png")
// Set the bucket to my-bucketname.
policy.setBucket("post-policy-test")

var expires = new Date
expires.setSeconds(24 * 60 * 60 * 10) // 10 days
policy.setExpires(expires)

policy.setContentLengthRange(1, 1024*1024) // Min upload length is 1KB Max upload size is 1MB

policy.setUserMetaData({
"sph-meta": 'sph-value',
"filename-meta": 'My* Docume nt.json'
})

policy.setContentType('image/png')

// this seems to cause trouble in curl.
// while the following sends the metadata. it is stored in unexpected format in server.
// policy.setContentDisposition(`attachment; filename="Cust_Mobile_View.png"`)


s3Client.presignedPostPolicy(policy, function(e, data) {
if (e) return console.log(e)
var curl = []
curl.push(`curl ${data.postURL} -X POST `)

for (var key in data.formData) {
if (data.formData.hasOwnProperty(key)) {
var value = data.formData[key]
curl.push(`-F ${key}='${value}'`)
}
}
// Print curl command to upload files.
// curl.push('-F file=@"/Pictures/Cust_Mobile_View.png"')
curl.push('-F file=@"<FILE_PATH>"')
console.log(curl.join(' '))
})

}

testViaHttpClient()

//generateCurl()