Skip to content

Commit

Permalink
Upload to Cloudflare R2
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlu committed Sep 11, 2023
1 parent 1c879df commit 45fef7b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/upload_s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ jobs:
AWS_SECRET: ${{ secrets.AWS_SECRET }}
TMDB_KEY: ${{ secrets.TMDB_KEY }}
OMDB_KEY: ${{ secrets.OMDB_KEY }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_ACCESS_KEY_ID: ${{ secrets.CLOUDFLARE_ACCESS_KEY_ID }}
CLOUDFLARE_SECRET_ACCESS_KEY: ${{ secrets.CLOUDFLARE_SECRET_ACCESS_KEY }}
CLOUDFLARE_BUCKET: ${{ secrets.CLOUDFLARE_BUCKET }}
- name: Store
uses: actions/upload-artifact@v3
with:
Expand Down
9 changes: 8 additions & 1 deletion bin/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Promise = require('bluebird')
const moment = require('moment')
const Index = require('../index')
const s3 = require('../lib/s3')
const r2 = require('../lib/r2')
const json2csv = require('../lib/json2csv')
const fs = Promise.promisifyAll(require('fs'))

Expand All @@ -23,7 +24,13 @@ const build = function (listBuilder, filename, opts = {}) {
count: movies.length
})

return s3.upload(this.filename, JSON.stringify(movies))
const jsonMovies = JSON.stringify(movies)

return Promise.all([
s3.upload(this.filename, jsonMovies),
r2.upload(this.filename, jsonMovies)
])
return
})
}

Expand Down
6 changes: 5 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ const config = {
AWS_BUCKET: 'popular-movies',
AWS_KEY: '',
AWS_SECRET: '',
OMDB_KEY: ''
OMDB_KEY: '',
CLOUDFLARE_ACCOUNT_ID: '',
CLOUDFLARE_ACCESS_KEY_ID: '',
CLOUDFLARE_SECRET_ACCESS_KEY: '',
CLOUDFLARE_BUCKET: ''
}

module.exports = _.pick(_.assign({}, config, process.env), _.keys(config))
33 changes: 33 additions & 0 deletions lib/r2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const Promise = require('bluebird')
const AWS = require('aws-sdk')
const zlib = require('zlib')
const config = require('../config')

const gzip = Promise.promisify(zlib.gzip)

AWS.config.setPromisesDependency(Promise)

const s3 = new AWS.S3({
endpoint: `https://${config.CLOUDFLARE_ACCOUNT_ID}.r2.cloudflarestorage.com`,
accessKeyId: config.CLOUDFLARE_ACCESS_KEY_ID,
secretAccessKey: config.CLOUDFLARE_SECRET_ACCESS_KEY,
signatureVersion: 'v4'
})

// module.exports = s3

module.exports.upload = async function (key, data) {
const compressedData = await gzip(data)

await s3
.putObject({
Bucket: config.CLOUDFLARE_BUCKET,
Key: key,
ContentType: 'application/json',
Body: compressedData,
ACL: 'public-read',
ContentEncoding: 'gzip',
CacheControl: 'max-age=43200'
})
.promise()
}

0 comments on commit 45fef7b

Please sign in to comment.