简体   繁体   English

在浏览器中查看而不是下载 Google Cloud Storage 文件

[英]View and not download Google Cloud Storage files in browser

I'am working with NodeJs/ express to create a POST api endpoint to upload files to google cloud storage and returning a public URL like this : https://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME]我正在与 NodeJs/express 合作创建一个 POST api 端点以将文件上传到谷歌云存储并返回一个公共 URL,如下所示: https ://storage.googleapis.com/[BUCKET_NAME]/[OBJECT_NAME ]

When the upload is done, I get the URL and when I open it , the file is downloaded directly (image, pdf etc...)上传完成后,我得到了 URL,当我打开它时,直接下载文件(图像、pdf 等...)

Is there a way to view and open it in the browser ?有没有办法在浏览器中查看和打开它?

here is my upload function :这是我的上传功能:

const uploadImage = (file) => new Promise((resolve, reject) => {
  const { originalname, buffer } = file

  const blob = bucket.file(originalname.replace(/ /g, "_"))
  const blobStream = blob.createWriteStream({
    resumable: false
  })

  blobStream.on('finish', () => {
    const publicUrl = format(
      `https://storage.googleapis.com/${bucket.name}/${blob.name}`
    )
    resolve(publicUrl)
  })
  .on('error', () => {
    reject(`Unable to upload image, something went wrong`)
  })
  .end(buffer)

})

Thanks谢谢

If the URL looks like the code above, https://storage.googleapis.com/bucketName/objectName , a browser should be able to view it directly, so long as a few conditions are in place:如果 URL 类似于上面的代码https://storage.googleapis.com/bucketName/objectName ,浏览器应该可以直接查看它,只要满足几个条件:

  • The object's contentType is set appropriately.对象的 contentType 设置适当。 If you don't specify a type, the default is application/octet-stream , and web browsers will probably decide to just download such an object rather than displaying it in some form.如果您不指定类型,默认值为application/octet-stream ,Web 浏览器可能会决定只下载这样的对象而不是以某种形式显示它。
  • The object's metadata does not override the contentDisposition.对象的元数据不会覆盖 contentDisposition。 It's possible to force objects to be downloaded as attachments by setting that property to something like attachment; filename=foo.txt通过将该属性设置为诸如attachment; filename=foo.txt东西,可以强制将对象作为附件下载attachment; filename=foo.txt attachment; filename=foo.txt . attachment; filename=foo.txt
  • The object is either publicly viewable or appropriate credentials are passed in the GET request.该对象可以公开查看,或者在 GET 请求中传递了适当的凭据。 This is not the default setting.这不是默认设置。 When you upload the object, you'll need to explicitly note that the ACL should allow the group allUsers read permission.当您上传对象时,您需要明确指出 ACL 应允许组allUsers读取权限。 Alternately, you could set the default object ACL property of the bucket to include that permission.或者,您可以设置存储桶的默认对象 ACL 属性以包含该权限。

In your case, the object is downloading successfully, so it's not the ACL issue, and if you don't know about the contentDisposition setting, then it's probably problem #1.在你的情况下,对象下载成功,所以这不是 ACL 问题,如果你不知道 contentDisposition 设置,那么它可能是问题 #1。 Make sure you specify a reasonable content type for the object.确保为对象指定合理的内容类型。

Example:例子:

const blobStream = blob.createWriteStream({
  resumable: false
  contentType: "text/html"
})

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

相关问题 允许用户从谷歌云存储下载单个和批量文件 - Alllow users to download single & bulk files from google cloud storage 如何在 Google Cloud Function 的 /tmp 文件夹中下载文件,然后将其上传到 Google Cloud Storage - How to download files in /tmp folder of Google Cloud Function and then upload it in Google Cloud Storage 重命名谷歌云存储中的文件? - renaming files in Google Cloud Storage? 如何从谷歌云存储下载文件? - How to download file from Google Cloud Storage? Google Cloud Storage:下载部分音频文件 - Google Cloud Storage: Download partial audio file 使用流星将图像文件从Google云存储桶下载到IOS本地存储 - Download image files from google cloud storage bucket to IOS localstorage using meteor 使用 NodeJS 从 Google Cloud Storage 中的一组路径中检索和下载所有文件 - Retrieve and download all files from a set of paths in Google Cloud Storage using NodeJS 使用Node.js存档器下载多个Google云存储文件并将其压缩 - Using nodejs archiver to download multiple google cloud storage files and zip them 从Google Cloud Storage下载多个文件(使用node.js) - Download multiple files from Google Cloud Storage (using node.js) 可读在节点谷歌云存储下载方法中不可异步迭代 - readable is not async iterable in Node Google Cloud Storage download method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM