bcascone
bcascone

Reputation: 139

Looking to obtain file information using Google Cloud functions.

I am attempting to gather some data on some files that I am receiving, I am looking for File size, number of records, date file received. These are .CSV files. At this point I have a function that will take a file that lands in the Bucket and pick up this file and move it to a new bucket. I am assuming that there are some sort of commands that can get this metadata from the file I am already grabbing. Ultimately I would like to write these results to a file, or table or an e-mail every time this file arrives.

A note this is my very first Google cloud project and I have absolutely zero Java coding background. here is the code I have so far:

Any advice is appreciated.

exports.CopySomewhereElse = (event, callback) => {
  const file = event.data;
  const context = event.context;

const Storage = require('@google-cloud/storage');

 // Creates a client
  const storage = new Storage();

  const srcBucketName = file.bucket;
  const srcFilename = file.name;
  const destBucketName = 'somewhere_else';
  const destFilename = file.name;

  // Copies the file to the other bucket
 storage
    .bucket(srcBucketName)
    .file(srcFilename)
    .copy(storage.bucket(destBucketName).file(destFilename))
    .then(() => {
      console.log(
        `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.`
      );
    })
    .catch(err => {
       console.error('ERROR:', err);
    });

  callback();
};

Upvotes: 0

Views: 139

Answers (1)

bcascone
bcascone

Reputation: 139

I was able to find the solution for this:

It was as easy as File.size and File.TimeCreated

exports.CopySomewhereElse = (event, callback) => {
  const file = event.data;
  const context = event.context;

  const Storage = require('@google-cloud/storage');

  // Creates a client
  const storage = new Storage();

  const srcBucketName = file.bucket;
  const srcFilename = file.name;
  const destBucketName = 'somewhere_else';

  const destFilename = file.name;
  const filesize = file.size;
  const FileName = file.name;
  const FiletimeModified = file.LastModified;
  const FileID = file.ID;
  const FiletimeCreated = file.timeCreated


  //const fileContentDisposition = file.contentDisposition;



  // Copies the file to the other bucket
  storage
    .bucket(srcBucketName)
    .file(srcFilename)
    .copy(storage.bucket(destBucketName).file(destFilename))
    .then(() => {
      console.log
                (
                    `gs://${srcBucketName}/${srcFilename} copied to gs://${destBucketName}/${destFilename}.|${filesize}|${FileName}|${FiletimeCreated}|${srcBucketName}`
                )
            //  (
            //      `${filesize}`
            //  )
    ;
    })
    .catch(err => {
      console.error('ERROR:', err);
    });

  callback();
};

Upvotes: 1

Related Questions