Cma*_*mag 2 go google-cloud-storage google-cloud-platform
各位,为什么存储在 Google 存储桶中的可公开访问的对象的下载方式很重要?
https://storage.cloud.google.com/convertedexcelfiles/test.png
Run Code Online (Sandbox Code Playgroud)
使用诸如wget或curl... 之类的工具似乎会混淆文件。
$ wget https://storage.cloud.google.com/convertedexcelfiles/test.png
...
$ ls -all -h
56K Feb 2 01:32 test.png
Run Code Online (Sandbox Code Playgroud)
对于值得信赖的人来说也是如此:
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
}
Run Code Online (Sandbox Code Playgroud)
唯一可靠的方法是使用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
Run Code Online (Sandbox Code Playgroud)
如果是这种情况,如何获取可靠的链接?
正如人们所提到的,该文件的内容是 html...经过更仔细的查看,以下内容是显而易见的:
正如您所看到的,这两个 URL 略有不同。
https://storage.cloud.google.com/convertedexcelfiles/test.png https://storage.googleapis.com/convertedexcelfiles/test.png
第二个有效……我有一种奇怪的感觉,其他不太懂技术的人会在这方面度过一段糟糕的时光。凌晨3点,这真是令人愤怒。我本应该简单地捕获该文件...但谁会想到呢!谷歌的用户界面做得很棒……见鬼?
| 归档时间: |
|
| 查看次数: |
1318 次 |
| 最近记录: |