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

问题描述 投票:0回答:1

我正在尝试收集我收到的一些文件的一些数据,我正在寻找文件大小,记录数,收到的日期文件。这些是.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();
};
google-cloud-platform google-cloud-storage google-cloud-functions
1个回答
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();
};
© www.soinside.com 2019 - 2024. All rights reserved.