我正在尝试将服务帐户密钥包含到我的存储功能中,以便能够通过遵循此处的过时示例来获取长期存在的签名URL
我已从IAM下载了JSON格式的密钥。我试图将其保存在我的功能旁边
-functions / storage / resizeProfileImg.js
-functions / storage / service-account-credentials.json
-functions / index.js
-functions / admin.js
其中resizeProfileImg.js是我的函数,并且这样调用
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: './service-account-credentials.json'})
但是在部署后,当函数被触发时,我得到一个错误
错误:ENOENT:没有此类文件或目录,请打开“ /srv/service-account-credentials.json”
我甚至试图像这样将其添加为常量
const serviceAccountCredentials = require('./accountKey/service-account-credentials.json')
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: serviceAccountCredentials})
但是我得到一个错误
TypeError:路径必须是字符串。已收到{类型:“ service_account”,...
这是我完整的功能
const {
db, storageRef
} = require('../../admin')
const projectId = "myProjectId"
const { Storage } = require('@google-cloud/storage');
const storage = new Storage({ projectId: projectId ,keyFilename: './service-account-credentials.json'})
const os = require('os');
const fs = require('fs');
const path = require('path');
const spawn = require('child-process-promise').spawn
const JPEG_EXTENSION = '.jpg'
exports.handler = ((object) => {
const bucket = object.bucket;
const contentType = object.contentType;
const filePath = object.name
const JPEGFilePath = path.normalize(path.format({ dir: path.dirname(filePath), name: 'profileImg', ext: JPEG_EXTENSION }))
const destBucket = storage.bucket(bucket)
const tempFilePath = path.join(os.tmpdir(), path.basename(filePath))
const tempLocalJPEGFile = path.join(os.tmpdir(), path.basename(JPEGFilePath))
const metadata = {
contentType: contentType
}
const uid = filePath.split("/").slice(1, 2).join("")
const d = new Date()
const date = new Date(d.setFullYear(d.getFullYear() + 200)).toString()
if (!object.contentType.startsWith('image/')) {
return destBucket.file(filePath).delete().then(() => {
console.log('File is not an image ', filePath, ' DELETED')
return null
});
}
if (object.metadata.modified) {
console.log('Image processed')
return null
}
return destBucket.file(filePath).download({
destination: tempFilePath
})
.then(() => {
console.log('The file has been downloaded to', tempFilePath)
return spawn('convert', [tempFilePath, '-resize', '100x100', tempLocalJPEGFile])
}).then(() => {
console.log('JPEG image created at', tempLocalJPEGFile)
metadata.modified = true
return destBucket.upload(tempLocalJPEGFile,
{
destination: JPEGFilePath,
metadata: { metadata: metadata }
})
}).then(() => {
console.log('JPEG image uploaded to Storage at', JPEGFilePath)
return destBucket.file(filePath).delete()
}).then(() => {
console.log('Original file deleted', filePath)
const logo = storageRef.file(JPEGFilePath)
return logo.getSignedUrl({ action: 'read', expires: date })
}).then((url) => {
const newRef = db.collection("user").doc(uid)
return newRef.set({
profile: { profileImg: url[0] }
}, {
merge: true
})
}).then(() => {
fs.unlinkSync(tempFilePath);
fs.unlinkSync(tempLocalJPEGFile)
console.log(uid, 'user database updated ')
return null
})
})
任何想法如何正确执行此操作
答案 0 :(得分:1)
在Cloud Functions中,当前目录.
不在源文件所在的位置。这是功能文件夹的部署位置。由于您的凭据文件位于名为“存储”的子目录中,因此需要在路径中使用该文件。
const serviceAccountCredentials = require('./storage/service-account-credentials.json')