go - 谷歌存储文件下载
问题描述
伙计们,为什么要下载存储在 Google 存储桶中的可公开访问的对象?
https://storage.cloud.google.com/convertedexcelfiles/test.png
使用诸如wget
或curl
... 之类的工具似乎会混淆文件。
$ wget https://storage.cloud.google.com/convertedexcelfiles/test.png
...
$ ls -all -h
56K Feb 2 01:32 test.png
值得信赖的人也是如此:
package main
import (
"io"
"net/http"
"os"
)
func main() {
fileUrl := "https://storage.cloud.google.com/convertedexcelfiles/test.png"
if err := DownloadFile("test.png", fileUrl); err != nil {
panic(err)
}
}
// DownloadFile will download a url to a local file. It's efficient because it will
// write as it downloads and not load the whole file into memory.
func DownloadFile(filepath string, url string) error {
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Write the body to file
_, err = io.Copy(out, resp.Body)
return err
}
似乎可靠的唯一方法是使用gsutil
或浏览到 gcp 控制台。想法?
可能是因为它返回 a302
吗?
curl -I https://storage.cloud.google.com/convertedexcelfiles/test.png ~/Downloads/transforms/tmp
HTTP/2 302
content-type: application/binary
location: https://accounts.google.com/ServiceLogin?service=cds&passive=1209600&continue=https://storage.cloud.google.com/convertedexcelfiles/test.png&followup=https://storage.cloud.google.com/convertedexcelfiles/test.png
content-length: 0
date: Sun, 02 Feb 2020 06:33:13 GMT
server: ESF
x-xss-protection: 0
x-frame-options: SAMEORIGIN
x-content-type-options: nosniff
alt-svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000
如果是这样的话,如何获取可靠的链接?
解决方案
正如人们所提到的,该文件的内容是 html.... 仔细查看后,以下内容是显而易见的:
如您所见,这 2 个 URL 略有不同。
https://storage.cloud.google.com/convertedexcelfiles/test.png https://storage.googleapis.com/convertedexcelfiles/test.png
第二个工作......我有一种奇怪的感觉,其他人,不太懂技术的人会在这方面度过一段糟糕的时光。凌晨 3 点,这令人愤怒。我应该只是简单地整理文件......但谁会预料到它!在用户界面谷歌上做得很好......见鬼?
推荐阅读
- pandas - 如何将我的数据框写为每个给定列的值一个文件?
- c++ - 将枚举类的多个位值传递给函数
- c# - 为什么 BCL 对许多方法使用有符号整数,而在许多情况下,无符号类型更合适?
- python - 从 Pandas 系列创建 Wordcloud
- c# - 从 foreach 语句插入插入数据,反之亦然
- node.js - puppeteerexposeFunction后如何关闭浏览器
- amazon-web-services - 在 apache 而不是 ELB 处终止 SSL
- node.js - npx create-next-app 在 npm run dev、npm start 和 npm run build 上产生错误
- context-free-grammar - 在这个例子中如何得到第二个最左边的推导?
- python - 是否可以为 numpy 数组创建弹出输入窗口?