繁体   English   中英

如何使用节点客户端库以不同的名称上传本地文件?

[英]How to upload a local file under a different name with the node client library?

Google Cloud的python客户端库界面允许您指定远程Blob名称(即/my/file.txt > gs://bucket/newName.txt )。 但是,我在节点客户端库中看不到执行此操作的方法。

我已经在python中运行了该示例,但是我依赖能够在时间戳上在存储服务器上添加文件名。 由于(AFAIK)节点库仅使用本地文件名,因此出现了问题。

Python示例 -具有源/目标名称

def upload_blob(bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    storage_client = storage.Client()
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print('File {} uploaded to {}.'.format(
        source_file_name,
        destination_blob_name))

节点示例 -使用本地文件名

// Uploads a local file to the bucket
await storage.bucket(bucketName).upload(filename, {
  // Support for HTTP requests made with `Accept-Encoding: gzip`
  gzip: true,
  metadata: {
    // Enable long-lived HTTP caching headers
    // Use only if the contents of the file will never change
    // (If the contents will change, use cacheControl: 'no-cache')
    cacheControl: 'public, max-age=31536000',
  },
});

有什么方法可以指定节点客户端库上传的文件名是什么?

谢谢。

使用节点SDK,您可以在传递的选项对象中指定目标文件作为要上传的第二个对象。 API文档中有一个有关upload()的示例。

storage.bucket(bucketName).upload(localFilePath, {
    destination: remoteFilePath
})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM