Monday, 12 June 2017

Download and upload picture gives me 0 bytes when using array

This is my code:

exports.resizeCardSet = functions.storage.object().onChange(event => {
const object = event.data; // The Storage object.
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const resourceState = object.resourceState; // The resourceState is 'exists' or 'not_exists' (for file/folder deletions).
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
const fileName = filePath.split('/').pop();
if (fileName.startsWith('thumb_')) {
  console.log('Already a Thumbnail.');
  return;
}
if (!contentType.startsWith('image/')) {
  console.log('This is not an image.');
  return;
}
if (resourceState === 'not_exists') {
  return;
}
if (resourceState === 'exists' && metageneration > 1) {
  console.log('This is a metadata change event.');
  return;
}
const bucket = gcs.bucket(fileBucket);
var fileNames = []
var promisesDownloads = []
var i = 0
while (i < 1){
    fileNames.push(`/tmp/${fileName}`)
    console.log("downloaded file to " + fileNames[i])
    promisesDownloads.push(bucket.file(filePath).download({
  destination: fileNames[i]
        })
    )
            i += 1
}
return Promise.all([promisesDownloads]).then(() => {
      var promisesUpload = []
      const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, '$1thumb_$2');
      var i = 0
while (i < fileNames.length){
    console.log('uploaded file to: ' + fileNames[i]);
    promisesUpload.push(bucket.upload(fileNames[i], {
      destination: thumbFilePath
    }))
    i += 1
      }
    return Promise.all([promisesUpload]);
  });
})

It results in 0 errors, and my log says this:

enter image description here

But the output of my uploaded picture is 0 bytes. When I did not used an array, it worked, but I need to do multiple downloads so I tried it now in an array which has only 1 index.

I hope someone can help.



via J. Doe

No comments:

Post a Comment