【问题标题】:Looking to obtain file information using Google Cloud functions.希望使用 Google Cloud 功能获取文件信息。
【发布时间】:2018-02-27 14:52:21
【问题描述】:

我正在尝试收集有关我收到的某些文件的一些数据,我正在寻找文件大小、记录数、收到文件的日期。这些是 .CSV 文件。在这一点上,我有一个函数,它将获取一个落在 Bucket 中的文件并拿起这个文件并将其移动到一个新的 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();
};

【问题讨论】:

  • 我想我找到了一个 NPM 包,可以满足我的需求。它位于以下站点:npmjs.com/package/gcp-metadata 我能够安装该包...但我不知道如何测试或使用它...
  • 很抱歉在这里评论不同的问题(使用 sendgrid 和谷歌云平台发送电子邮件) - 不想被以下消息暴露太多 - 我正处于私人测试阶段,产品正是这样做的- 如果有兴趣 - 通过 LinkedIn 联系我

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


【解决方案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();
};

【讨论】:

  • 你也可以看看NodeJS Client Library for Cloud Storage的官方文档,具体可以在getMetadata() method找到可用的元数据信息。
  • 另外,只要您能够(通常是在您最初发布问题后 2 天),请接受您自己的答案,以便社区确认该问题已解决。谢谢!
猜你喜欢
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多