谷歌云存储图像公共链接到img src [英] Google cloud storage image public link to img src

查看:135
本文介绍了谷歌云存储图像公共链接到img src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我调查了相关问题,但未找到答案.我想在我的网站上提供来自Google云端存储的公共链接,但是它们仅作为下载链接提供.我无法将它们作为img src加载.由于CORS,我无法从网页js中异步下载图片.有解决方法吗?我想知道我是否可以最初将它们异步定义为下载链接,并强制onclick下载它们,然后使用文件阅读器将blob用作img src.

Hi I have looked into related questions, but have been unsuccessful in finding an answer. I would like to serve public links from google cloud storage on my website, but they are only available as download links. I am unable to place them as img src to load them. I cannot download the image async from my webpage js because of CORS. Is there a workaround? I was wondering whether I could asynchronously define them as download links initially and force onclick to download them then use file reader to get the blob to use as img src.

更新:我发现存在Chromium问题.我将Firefox与CORS_Everywhere一起使用,并且请求有效.网站启用后,图像会出现相同的问题吗?还是CORS只是不接受本地主机?还是取决于浏览器?

UPDATE : I figured out that there was a Chromium issue. I used Firefox with CORS_Everywhere and the requests works. Once my website is live, will I have the same issue for the images? OR is it that CORS just doesn't accept localhost? OR is it browser dependent?

谢谢, 赶时间的人

推荐答案

我不确定您要做什么.听起来您想在<img>标记中显示图像,其中源位于GCS中.为此,您不需要任何形式的CORS政策或Javascript技巧,因此我可能会误解您的问题.

I'm not entirely sure what you're trying to do. It sounds like you'd like to display images in an <img> tag where the source is in GCS. You shouldn't need any sort of CORS policy or Javascript trickery for that, so I may misunderstand your question.

要在网站上包含GCS图像,请确保该图像是公开可见的,然后仅添加一个图像标签,如下所示:<img src="https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME" />.

To include GCS images on a website, make sure the image is publicly visible, then just include an image tag like this: <img src="https://storage.googleapis.com/BUCKET_NAME/OBJECT_NAME" />.

根据评论的答案进行

如果上载对象时未指定Content-Type,则GCS默认为通用的"application/octet-stream". Web浏览器通常默认保存application/octet-stream响应.如果要通过网络提供服务,请确保JPEG图像的内容类型为"image/jpeg".

If you upload an object without specifying a Content-Type, GCS defaults to a generic "application/octet-stream." Web browsers generally default to saving application/octet-stream responses. Make sure JPEG images have the Content-Type "image/jpeg" if you want to serve them via the web.

另一个警告:

还有另一条使用https://storage.cloud.google.com/路径的路径,但是该路径用于用户使用其自己帐户的Google Cookie进行身份验证,因此在提供公共内容时,您通常不希望依赖该路径.使用https://storage.googleapis.com.

There is another path using the https://storage.cloud.google.com/ path, but that one is for users authenticating with their own account's Google cookies, and you generally don't wan to rely on that when serving public content. Use https://storage.googleapis.com.

这篇关于谷歌云存储图像公共链接到img src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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