"Firebase存储",用于图像-但是,获取实际的URL吗? [英] "Firebase Storage", for images - but, get the actual URL?

查看:58
本文介绍了"Firebase存储",用于图像-但是,获取实际的URL吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将图像发送到"Firebase存储"系统...

sr ... a StorageReference (ie, FIRStorageReference)
let task = sr.putData(data)
task.observe(.success) { [index] snapshot in
    guard let downloadURL: URL = snapshot.metadata?.downloadURL()
    else { .. err }

但是,最终结果是URL,如下所示:

https://firebasestorage.googleapis.com/v0/b/someapp.com/o/0.jpg?alt=media&token=113150a1-8152-666-666-fd00dc9e8734

它绝对完美地工作-但是-

该URL仅仅是不是jpeg的URL -仅仅是文件的URL.点击它会导致您获取jpeg的文件下载.

这似乎不可思议-使用所有其他在线存储(AWS等),您只会得到"jpeg的实际URL".

这里有解决方案吗?


正如BC在下面问到的那样,查看标题:

$ cat headers.txt
HTTP/1.1 200 OK
X-GUploader-UploadID: AEnB2Ur3b5CNUIWWZdwn-666slsPqTjJT3GF83m6sJ-mzx...666
Expires: Sat, 29 Jul 2017 23:22:38 GMT
Date: Sat, 29 Jul 2017 23:22:38 GMT
Cache-Control: private, max-age=0
Last-Modified: Sat, 29 Jul 2017 18:55:21 GMT
ETag: "9e4491f86bd78ee62d241d962893f666"
x-goog-generation: 1501354521940666
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 8992
x-goog-meta-firebaseStorageDownloadTokens: cd9e7a4b-6666-666-b6b8-5a700bed2e64
Content-Type: application/octet-stream
Content-Disposition: inline; filename*=utf-8''0.jpg
x-goog-hash: crc32c=z72qJQ==
x-goog-hash: md5=nkSR+GvXjuYtJB2WKJP6666==
x-goog-storage-class: STANDARD
Accept-Ranges: bytes
Content-Length: 8992
Server: UploadServer
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"

解决方案

您的代码正在将原始二进制数据上传到Storage中,而不考虑其内容类型.使用putData时,Storage SDK不会尝试解释该NSData Blob的内容.但是,如果使用putFile,则将推断内容类型 .

如果您需要使用putData,请尝试指定一些元数据以及通过putData:metadata:上传的文件.

在Firebase控制台中,您可以通过选择文件来检查其上载的内容类型.它也应该在右侧面板中读取image/jpeg.

Sending an image to the "Firebase Storage" system ...

sr ... a StorageReference (ie, FIRStorageReference)
let task = sr.putData(data)
task.observe(.success) { [index] snapshot in
    guard let downloadURL: URL = snapshot.metadata?.downloadURL()
    else { .. err }

However, the end result is URLs, which are like this:

https://firebasestorage.googleapis.com/v0/b/someapp.com/o/0.jpg?alt=media&token=113150a1-8152-666-666-fd00dc9e8734

It works absolutely perfectly - however -

that URL is simply not a URL of a jpeg - it's just a URL of a file. Clicking on it results in you getting a file download of the jpeg.

This seems incredible - with all other online storage (AWS etc), you just get "an actual URL of a jpeg".

Is there a solution here?


As BC asked below, looking at the headers:

$ cat headers.txt
HTTP/1.1 200 OK
X-GUploader-UploadID: AEnB2Ur3b5CNUIWWZdwn-666slsPqTjJT3GF83m6sJ-mzx...666
Expires: Sat, 29 Jul 2017 23:22:38 GMT
Date: Sat, 29 Jul 2017 23:22:38 GMT
Cache-Control: private, max-age=0
Last-Modified: Sat, 29 Jul 2017 18:55:21 GMT
ETag: "9e4491f86bd78ee62d241d962893f666"
x-goog-generation: 1501354521940666
x-goog-metageneration: 1
x-goog-stored-content-encoding: identity
x-goog-stored-content-length: 8992
x-goog-meta-firebaseStorageDownloadTokens: cd9e7a4b-6666-666-b6b8-5a700bed2e64
Content-Type: application/octet-stream
Content-Disposition: inline; filename*=utf-8''0.jpg
x-goog-hash: crc32c=z72qJQ==
x-goog-hash: md5=nkSR+GvXjuYtJB2WKJP6666==
x-goog-storage-class: STANDARD
Accept-Ranges: bytes
Content-Length: 8992
Server: UploadServer
Alt-Svc: quic=":443"; ma=2592000; v="39,38,37,36,35"

解决方案

Your code is uploading raw binary data to Storage without regarding its content type. The Storage SDK is not going to attempt to interpret the contents of that NSData blob when using putData. However, the content type will be inferred if using putFile.

If you need to use putData, instead try specifying some metadata along with the upload with putData:metadata:.

In the Firebase console, you can check the content type of the file uploaded by selecting it. It should read image/jpeg in the right hand panel as well.

这篇关于"Firebase存储",用于图像-但是,获取实际的URL吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆