From cd0b800ff9a13365cb36ff03282a5942b923b664 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Tue, 13 Jul 2021 14:53:23 +0100 Subject: [PATCH] Add file download to node example (#530) * Add file download to node example * Set validation: false * Set X-Goog-Hash header on download response * Add both crc32c and md5 * Remove fixed comment. * Don't set empty content-type header on download Temporary workaround for #532 so we can get this PR in. Co-authored-by: francisco souza <108725+fsouza@users.noreply.github.com> Co-authored-by: francisco souza --- examples/node/docker-compose.yaml | 9 +++++++++ examples/node/index.js | 11 +++++++++-- examples/node/package.json | 1 + fakestorage/object.go | 5 ++++- 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 examples/node/docker-compose.yaml diff --git a/examples/node/docker-compose.yaml b/examples/node/docker-compose.yaml new file mode 100644 index 0000000000..a125dcec81 --- /dev/null +++ b/examples/node/docker-compose.yaml @@ -0,0 +1,9 @@ +services: + storage: + image: fsouza/fake-gcs-server + build: ../../ + ports: + - 8080:8080 + volumes: + - ../data:/data + command: ["-scheme", "http", "-port", "8080", "-external-url", "http://localhost:8080", "-backend", "memory"] diff --git a/examples/node/index.js b/examples/node/index.js index ec984e2556..e96e264259 100644 --- a/examples/node/index.js +++ b/examples/node/index.js @@ -1,4 +1,4 @@ -async function listBuckets() { +async function run() { // [START storage_list_buckets] // Imports the Google Cloud client library const { Storage } = require("@google-cloud/storage"); @@ -16,9 +16,16 @@ async function listBuckets() { console.log(bucket.id); }); // [END storage_list_buckets] + + const [content] = await storage.bucket('sample-bucket') + .file('some_file.txt') + .download(); + console.log("Contents:") + console.log(content.toString()) } -listBuckets().catch((err) => { + +run().catch((err) => { console.error(err); process.exit(1); }); diff --git a/examples/node/package.json b/examples/node/package.json index da92e6971b..7fdb0f8d7f 100644 --- a/examples/node/package.json +++ b/examples/node/package.json @@ -4,6 +4,7 @@ "description": "", "main": "index.js", "scripts": { + "run": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", diff --git a/fakestorage/object.go b/fakestorage/object.go index 7fc548c7b0..c7a40eba76 100644 --- a/fakestorage/object.go +++ b/fakestorage/object.go @@ -558,10 +558,13 @@ func (s *Server) downloadObject(w http.ResponseWriter, r *http.Request) { status = http.StatusPartialContent w.Header().Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, len(obj.Content))) } + if obj.ContentType != "" { + w.Header().Set(contentTypeHeader, obj.ContentType) + } w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Content-Length", strconv.Itoa(len(content))) - w.Header().Set(contentTypeHeader, obj.ContentType) w.Header().Set("X-Goog-Generation", strconv.FormatInt(obj.Generation, 10)) + w.Header().Set("X-Goog-Hash", fmt.Sprintf("crc32c=%s,md5=%s", obj.Crc32c, obj.Md5Hash)) w.Header().Set("Last-Modified", obj.Updated.Format(http.TimeFormat)) if obj.ContentEncoding != "" { w.Header().Set("Content-Encoding", obj.ContentEncoding)