相关疑难解决方法(0)

如何使用Node.js将base64编码图像(字符串)直接上传到Google Cloud Storage存储桶?

目前,我正在使用@ google-cloud/storage NPM软件包将文件直接上传到Google云端存储分区.这需要一些技巧,因为我只有图像的base64编码字符串.我必须:

  • 解码字符串
  • 将其另存为文件
  • 将文件路径发送到以下脚本以上传到Google云端存储
  • 删除本地文件

我想避免将文件存储在文件系统中,因为我使用的是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

24
推荐指数
5
解决办法
1万
查看次数

在没有文件的情况下将 JSON 字符串上传到 Google Cloud Storage

我的情况是当我从其他来源接收数据作为 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 的内容

javascript storage json node.js google-cloud-storage

14
推荐指数
1
解决办法
8616
查看次数

如何在python3.6中将'_io.BytesIO'转换为类似字节的对象?

我正在使用此函数解压缩正文或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”转换为类似字节的对象?

谢谢

python zlib typeerror bytesio python-3.x

7
推荐指数
3
解决办法
5729
查看次数