我正在尝试收集一些我收到的文件的数据,我正在寻找文件大小,记录数,收到的日期文件。这些是.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();
};
答案 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();
};