我有一个Google Cloud功能,可以获取网址的屏幕截图,并且需要将该图像从Google Cloud功能上传到Google Cloud Bucket
我尝试使用下面给出的代码上传它,但是它不起作用并给出错误
/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.takeScreenshot = (req, res) => {
const Screenshot = require('url-to-screenshot')
const Storage = require('@google-cloud/storage')
const fs = require('fs')
const bucketName="screenshot_bucket_mujahid"
new Screenshot('http://ghub.io/')
.width(800)
.height(600)
.capture()
.then(img =>
Storage
.bucket(bucketName)
.upload(img,"mujahid.jpg"))
console.log('open example.png')
})
let message = req.query.message || req.body.message || 'Hello World!';
res.status(200).send(message);
};
答案 0 :(得分:1)
您的代码似乎存在语法错误。代替:
.then(img =>
Storage
.bucket(bucketName)
.upload(img,"mujahid.jpg"))
console.log('open example.png')
})
编码可能是正确的:
.then(img =>
{
Storage
.bucket(bucketName)
.upload(img,"mujahid.jpg");
console.log('open example.png');
});
答案 1 :(得分:0)
尝试这样的事情:
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const bucketName = ' something ' // bucket you want to upload to
const fileName = ' some path ' // PATH of the file
await storage.bucket(bucketName).upload(filename, {
gzip: true,
metadata: {
cacheControl: 'no-cache'
},
});
因此,请注意,对于上传功能,您必须传递要上传文件的整个路径。
如果上传仍然无法进行,请检查GCS存储桶中是否没有权限问题。尝试make data public并重试上传。但是最有可能的是,如果您的文件没有上传并且没有错误,那么这部分代码可能仍然卡住了:
new Screenshot('http://ghub.io/')
.width(800)
.height(600)
.capture()