Skip to content

Commit

Permalink
Add file download to node example (#530)
Browse files Browse the repository at this point in the history
* 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 <fsouza@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 13, 2021
1 parent ca4df71 commit cd0b800
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
9 changes: 9 additions & 0 deletions examples/node/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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"]
11 changes: 9 additions & 2 deletions examples/node/index.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -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);
});
1 change: 1 addition & 0 deletions examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"run": "node index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
Expand Down
5 changes: 4 additions & 1 deletion fakestorage/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit cd0b800

Please sign in to comment.