Created
October 19, 2019 14:35
-
-
Save wozozo/d9a0b6834d1306d7b4c7bb2ac9428c96 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 puppeteer = require('puppeteer'); | |
const { Storage } = require('@google-cloud/storage'); | |
const url: string = ''; | |
const bucketName = ''; | |
const fileName = `hoge-${Date.now()}.png`; | |
(async () => { | |
const browser = await puppeteer.launch(); | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 1600, height: 950 }); | |
const response = await page.goto(url); | |
const element = await page.$('.hoge'); | |
if (element === null) { | |
throw new Error('`elements` is null.'); | |
} | |
await element.screenshot({ path: fileName }); | |
await uploadFile(bucketName, fileName); | |
await makePublic(bucketName, fileName); | |
await browser.close(); | |
})().catch(e => console.error(e)); | |
// https://github.com/googleapis/nodejs-storage/blob/859b3e03b6c5419c9d056f39c56dbf972df96c99/samples/files.js | |
async function uploadFileToGCS(bucketName: string, filename: string) { | |
// Uploads a local file to the bucket | |
try { | |
await storage.bucket(bucketName).upload(filename, { | |
// Support for HTTP requests made with `Accept-Encoding: gzip` | |
gzip: true, | |
// By setting the option `destination`, you can change the name of the | |
// object you are uploading to a bucket. | |
metadata: { | |
// Enable long-lived HTTP caching headers | |
// Use only if the contents of the file will never change | |
// (If the contents will change, use cacheControl: 'no-cache') | |
cacheControl: 'public, max-age=31536000', | |
}, | |
}); | |
console.log(`${filename} uploaded to ${bucketName}.`); | |
// [END storage_upload_file] | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
async function makePublic(bucketName: string, filename: string) { | |
// Makes the file public | |
try { | |
await storage | |
.bucket(bucketName) | |
.file(filename) | |
.makePublic(); | |
console.log(`gs://${bucketName}/${filename} is now public.`); | |
// [END storage_make_public] | |
} catch (e) { | |
console.error(e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment