如何在 Google Cloud Storage 中设置通过签名 url 上传的对象的内容类型?

问题描述 投票:0回答:1

我正在生成签名 URL 以使用节点脚本上传媒体文件。但是,当我检查存储中的文件时,文件元数据是错误的,就像它是多部分表单数据一样。从 GCP 存储控制台打印演示:

enter image description here

我需要将文件解释为媒体文件,例如打印:

enter image description here

我生成签名网址的代码:

import { GetSignedUrlConfig, Storage } from '@google-cloud/storage';
import { BucketProvider } from '@shared/providers/Bucket.provider';

export class CloudStorageProviderImp implements BucketProvider {
  private storage: Storage;

  constructor() {
    this.storage = new Storage({
      projectId: process.env.GCP_PROJECT_ID,
    });
  }

  public async generateUploadSignedUrl(
    bucketName: string,
    fileName: string,
    expireTimeInMinutes: number
  ): Promise<string> {
    const options: GetSignedUrlConfig = {
      version: 'v4',
      action: 'write',
      expires: this.formatExpires(expireTimeInMinutes),
      contentType: 'audio/wav'
    };

    const [url] = await this.storage
      .bucket(bucketName)
      .file(fileName)
      .getSignedUrl(options)
      

    return url;
  }

  private formatExpires(expireTimeInMinutes: number) {
    return Date.now() + expireTimeInMinutes * 60 * 1000;
  }
}


GCP 的文档很差,直到现在我还没有找到解决方案如何设置要正确上传的对象的元数据

node.js google-cloud-platform google-cloud-storage
1个回答
0
投票

尝试检查您的代码逗号和分号,与 docs 示例代码相比

  const options = {
    version: 'v4',
    action: 'write',
    expires: Date.now() + 15 * 60 * 1000, // 15 minutes
    contentType: 'application/octet-stream',
  };

  // Get a v4 signed URL for uploading file
  const [url] = await storage
    .bucket(bucketName)
    .file(fileName)
    .getSignedUrl(options);
© www.soinside.com 2019 - 2024. All rights reserved.