目前,我正在使用@ google-cloud/storage NPM软件包将文件直接上传到Google云端存储分区.这需要一些技巧,因为我只有图像的base64编码字符串.我必须:
我想避免将文件存储在文件系统中,因为我使用的是Google App Engine,如果删除操作因任何原因无效,我不想重载文件系统/留下垃圾文件.这就是我的上传脚本现在的样子:
// Convert the base64 string back to an image to upload into the Google Cloud Storage bucket
var base64Img = require('base64-img');
var filePath = base64Img.imgSync(req.body.base64Image, 'user-uploads', 'image-name');
// Instantiate the GCP Storage instance
var gcs = require('@google-cloud/storage')(),
bucket = gcs.bucket('google-cloud-storage-bucket-name');
// Upload the image to the bucket
bucket.upload(__dirname.slice(0, -15) + filePath, {
destination: 'profile-images/576dba00c1346abe12fb502a-original.jpg',
public: true,
validation: 'md5'
}, function(error, file) {
if (error) {
sails.log.error(error);
}
return res.ok('Image …Run Code Online (Sandbox Code Playgroud) javascript node.js google-cloud-storage google-cloud-platform
我的情况是当我从其他来源接收数据作为 JSON 字符串时,然后我想将此字符串上传到 Google Cloud Storage,而不将此字符串写入本地文件并上传此文件。有没有办法做到这一点。谢谢你。看起来像下面的这段代码
storage
.bucket(bucketName)
.upload(jsonString, { destination: 'folder/test.json' })
.then(() => {
console.log('success');
})
.catch((err) => {
console.error('ERROR:', err);
});
Run Code Online (Sandbox Code Playgroud)
所以我预计谷歌云存储会有一个文件 test.json 包含来自 jsonString 的内容
我正在使用此函数解压缩正文或HTTP响应(如果使用gzip对其进行了压缩,压缩或放气)。
def uncompress_body(self, compression_type, body):
if compression_type == 'gzip' or compression_type == 'compress':
return zlib.decompress(body)
elif compression_type == 'deflate':
compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)
compressed = compressor.compress(body)
compressed += compressor.flush()
return base64.b64encode(compressed)
return body
Run Code Online (Sandbox Code Playgroud)
但是python抛出此错误消息。
TypeError: a bytes-like object is required, not '_io.BytesIO'
Run Code Online (Sandbox Code Playgroud)
在这条线上:
return zlib.decompress(body)
Run Code Online (Sandbox Code Playgroud)
本质上,我如何从“ _io.BytesIO”转换为类似字节的对象?
谢谢
javascript ×2
node.js ×2
bytesio ×1
json ×1
python ×1
python-3.x ×1
storage ×1
typeerror ×1
zlib ×1