希望使用Google Cloud功能获取文件信息。

时间:2018-02-27 14:52:21

标签: google-cloud-platform google-cloud-storage google-cloud-functions

我正在尝试收集一些我收到的文件的数据,我正在寻找文件大小,记录数,收到的日期文件。这些是.CSV文件。在这一点上,我有一个函数,它将获取存储在Bucket中的文件并获取此文件并将其移动到新存储桶。我假设有一些命令可以从我已经抓取的文件中获取此元数据。最终,我想在每次收到此文件时将这些结果写入文件,表格或电子邮件。

注意这是我的第一个Google云项目,我的Java编码背景绝对没有。这是我到目前为止的代码:

任何建议表示赞赏。

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();
};

1 个答案:

答案 0 :(得分:1)

我能够找到解决方案:

就像File.size和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();
};