Last active
January 16, 2021 03:22
-
-
Save drwpow/46e46271a0c50df8862383f15d57949f to your computer and use it in GitHub Desktop.
Google Cloud Storage availability test: generate many random files, upload, then download
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const crypto = require("crypto"); | |
const fs = require("fs"); | |
const path = require("path"); | |
const { Storage } = require("@google-cloud/storage"); | |
const mkdirp = require("mkdirp"); | |
// settings | |
const FILES_TO_GENERATE = 1000; | |
const KEY_FILE = "/my/google/service/key.json"; | |
const storage = new Storage({ | |
keyFilename: KEY_FILE, | |
}); | |
const bucket = storage.bucket("test_availability_1"); | |
async function main() { | |
let successful = 0; | |
await Promise.all( | |
[...Array(FILES_TO_GENERATE)].map(async (_, n) => { | |
// generate random filename | |
let key = crypto.randomBytes(48).toString("base64"); // may contain '/'s | |
// insert 0–4 folders | |
for (i = 0; i < n % 5; i++) { | |
const randomChar = Math.floor(Math.random() * key.length); | |
key[randomChar] = "/"; | |
} | |
key = key.replace(/\/+/, "/"); // no double-slashes | |
key += ".txt"; // add ext | |
// write | |
const filename = path.join(__dirname, "tmp", key); | |
mkdirp.sync(path.dirname(filename)); | |
fs.writeFileSync( | |
filename, | |
crypto | |
.randomBytes(Math.floor(Math.random() * 100000)) // this will be ~100 KB max or so | |
.toString("base64") | |
); | |
try { | |
console.log(`[${n + 1}]: uploading`); | |
await bucket.upload(filename, { destination: key }); | |
console.log(`[${n + 1}]: uploaded`); | |
const [contents] = await bucket.file(key).download(); | |
console.log(`[${n + 1}]: downloaded`); | |
successful += 1; | |
} catch (err) { | |
console.error(err.toString()); | |
} | |
}) | |
); | |
console.log(`DONE: ${successful}/${FILES_TO_GENERATE} downloaded`); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment