Closed xabree closed 6 years ago
Hey @xabree,
You have the buffer containing your file data right in your params object -- you can access it by declaring a param named "file" in your function definition like this:
const aws = require('aws-sdk');
const s3 = aws.S3({
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
/**
* A basic Hello World function
* @param {buffer} file
* @returns {object}
*/
module.exports = (file, callback) => {
// Store file here
s3.putObject({
Bucket: process.env.S3_BUCKET_NAME,
Key: // What you want to store the image as
Body: file
}, (err, results) => {
if (err) {
return callback(err);
}
return callback(null, results);
});
};
We don't currently have a built in data layer, but the above example will store the file in S3 -- you can also use any other storage service you'd prefer. You should store your AWS keys in your StdLib service's env.json
file, then reference them as shown above.
Let me know if I can help further!
hey @jacoblee93 thank for response, I Already success uploaded buffer file to amazon s3, but image is always corrupt, can't display in browser.
var nm = Math.random().toString(36).substring(7);
s3.upload({
Bucket: 'uploads',
Key: nm+'.jpg',
ContentType:'image/jpeg',
Body: file,
ACL: 'public-read'
},function (resp) {
console.log(resp);
console.log('Successfully uploaded package.');
});
Actually the issue is still present. Uploaded files are all corrupt. Tried to upload files to box.com and all of them are saved corrupt and do not open.
Hey @intermundos,
Can you go into more detail on how exactly you're trying to do this? Sharing the code you're running would help.
@jacoblee93 Hi, thanks for quick response!
Sure, below is the endpoint. I send data from client with multipart data form.
Thanks a lot in advance.
/**
* @param {buffer} cvFile
* @param {string} cvName
* @param {string} cvExtension
* @returns {any} result
*/
module.exports = async (cvFile, cvName, cvExtension) => {
let result = await lib.box.files['@2.0.1'].content.create({
upload: {
attributes: {
name: `${cvName}.${cvExtension}`,
parent: {
id: '0',
},
},
file: cvFile,
},
});
return {
success: true,
};
};
Ok, thanks. Will look into it. It would be helpful, if possible, if you could give me an example of the client code/data file as well?
Thanks much!
Cant post here everything but will try to summarise
function send(params) {
const { cvFile, cvName, cvExtension } = params
const formData = new FormData()
formData.append('cvFile', cvFile) // taken from input[file] as target.files[0]
formData.append('cvName', cvName)
formData.append('cvExtension', cvExtension)
ky.post(url, { body: formData })
}
Let me know if you need further details.
UPD: .txt files sent to box are saved correctly. All other files get corrupted.
Hey @intermundos, we've pushed a fix. Can you confirm it's ok on your end? You shouldn't have to change anything.
@jacoblee93 Thanks much! Will test later today and let you know.
@jacoblee93
Had been busy much...
Now it's working fine.
Thanks a lot!
Excellent! Glad to hear that. Let us know if you have other questions - we're really active in our community Discord: https://discord.gg/autocode
how to store upload file / image, any idea ? Im trying to upload image using fetch post like this
we get the response from server request like this :