mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-07-30 18:30:01 +08:00
✨ Feature: migrate from aws-sdk v2 to v3
This commit is contained in:
@@ -29,8 +29,8 @@ const uploadFile = async () => {
|
||||
secretAccessKey: SECRET_KEY
|
||||
},
|
||||
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
|
||||
sslEnabled: true,
|
||||
region: 'us-east-1'
|
||||
tls: true,
|
||||
region: 'auto'
|
||||
}
|
||||
const client = new S3Client.S3Client(options)
|
||||
const parallelUploads3 = new Upload.Upload({
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// upload dist bundled-app to r2
|
||||
require('dotenv').config()
|
||||
const S3 = require('aws-sdk/clients/s3')
|
||||
|
||||
const S3Client = require('@aws-sdk/client-s3')
|
||||
const Upload = require('@aws-sdk/lib-storage')
|
||||
const pkg = require('../package.json')
|
||||
@@ -8,6 +8,7 @@ const configList = require('./config')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const yaml = require('js-yaml')
|
||||
const mime = require('mime-types')
|
||||
|
||||
const BUCKET = 'piclist-dl'
|
||||
const VERSION = pkg.version
|
||||
@@ -16,12 +17,15 @@ const ACCOUNT_ID = process.env.R2_ACCOUNT_ID
|
||||
const SECRET_ID = process.env.R2_SECRET_ID
|
||||
const SECRET_KEY = process.env.R2_SECRET_KEY
|
||||
|
||||
const s3 = new S3({
|
||||
const options = {
|
||||
credentials: {
|
||||
accessKeyId: SECRET_ID,
|
||||
secretAccessKey: SECRET_KEY
|
||||
},
|
||||
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
|
||||
accessKeyId: SECRET_ID,
|
||||
secretAccessKey: SECRET_KEY,
|
||||
signatureVersion: 'v4'
|
||||
})
|
||||
tls: true,
|
||||
region: 'auto'
|
||||
}
|
||||
|
||||
const removeDupField = path => {
|
||||
const file = fs.readFileSync(path, 'utf8')
|
||||
@@ -39,74 +43,87 @@ const removeDupField = path => {
|
||||
const uploadFile = async () => {
|
||||
try {
|
||||
const platform = process.platform
|
||||
if (configList[platform]) {
|
||||
let versionFileHasUploaded = false
|
||||
for (const [index, config] of configList[platform].entries()) {
|
||||
const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}`
|
||||
const distPath = path.join(__dirname, '../dist_electron')
|
||||
const versionFileName = config['version-file']
|
||||
console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`)
|
||||
const fileStream = fs.createReadStream(path.join(distPath, fileName))
|
||||
const options = {
|
||||
credentials: {
|
||||
accessKeyId: SECRET_ID,
|
||||
secretAccessKey: SECRET_KEY
|
||||
},
|
||||
endpoint: `https://${ACCOUNT_ID}.r2.cloudflarestorage.com`,
|
||||
sslEnabled: true,
|
||||
region: 'us-east-1'
|
||||
if (!configList[platform]) {
|
||||
console.warn('platform not supported!', platform)
|
||||
return
|
||||
}
|
||||
let versionFileHasUploaded = false
|
||||
for (const [index, config] of configList[platform].entries()) {
|
||||
const fileName = `${config.appNameWithPrefix}${VERSION}${config.arch}${config.ext}`
|
||||
const distPath = path.join(__dirname, '../dist_electron')
|
||||
const versionFileName = config['version-file']
|
||||
|
||||
console.log('[PicList Dist] Uploading...', fileName, `${index + 1}/${configList[platform].length}`)
|
||||
const fileStream = fs.createReadStream(path.join(distPath, fileName))
|
||||
const client = new S3Client.S3Client(options)
|
||||
|
||||
const parallelUploads3 = new Upload.Upload({
|
||||
client,
|
||||
params: {
|
||||
Bucket: BUCKET,
|
||||
Key: `${FILE_PATH}${fileName}`,
|
||||
Body: fileStream,
|
||||
ContentType: 'application/octet-stream',
|
||||
Metadata: {
|
||||
description: 'uploaded by PicList'
|
||||
}
|
||||
}
|
||||
const client = new S3Client.S3Client(options)
|
||||
const parallelUploads3 = new Upload.Upload({
|
||||
})
|
||||
parallelUploads3.on('httpUploadProgress', progress => {
|
||||
const progressBar = Math.round((progress.loaded / progress.total) * 100)
|
||||
process.stdout.write(`\r${progressBar}% ${fileName}`)
|
||||
})
|
||||
console.log('\n')
|
||||
await parallelUploads3.done()
|
||||
console.log(`${fileName} uploaded!`)
|
||||
|
||||
if (!versionFileHasUploaded) {
|
||||
console.log('[PicList Version File] Uploading...', versionFileName)
|
||||
let versionFilePath
|
||||
if (platform === 'win32') {
|
||||
versionFilePath = path.join(distPath, 'latest.yml')
|
||||
} else if (platform === 'darwin') {
|
||||
versionFilePath = path.join(distPath, 'latest-mac.yml')
|
||||
} else {
|
||||
versionFilePath = path.join(distPath, 'latest-linux.yml')
|
||||
}
|
||||
removeDupField(versionFilePath)
|
||||
const versionFileStream = fs.createReadStream(versionFilePath)
|
||||
const uploadVersionFileToRoot = new Upload.Upload({
|
||||
client,
|
||||
params: {
|
||||
Bucket: BUCKET,
|
||||
Key: `${FILE_PATH}${fileName}`,
|
||||
Body: fileStream,
|
||||
ContentType: 'application/octet-stream',
|
||||
Key: `${versionFileName}`,
|
||||
Body: versionFileStream,
|
||||
ContentType: mime.lookup(versionFileName),
|
||||
Metadata: {
|
||||
description: 'uploaded by PicList'
|
||||
}
|
||||
}
|
||||
})
|
||||
parallelUploads3.on('httpUploadProgress', progress => {
|
||||
const progressBar = Math.round((progress.loaded / progress.total) * 100)
|
||||
process.stdout.write(`\r${progressBar}% ${fileName}`)
|
||||
})
|
||||
console.log('\n')
|
||||
await parallelUploads3.done()
|
||||
console.log(`${fileName} uploaded!`)
|
||||
if (!versionFileHasUploaded) {
|
||||
console.log('[PicList Version File] Uploading...', versionFileName)
|
||||
let versionFilePath
|
||||
if (platform === 'win32') {
|
||||
versionFilePath = path.join(distPath, 'latest.yml')
|
||||
} else if (platform === 'darwin') {
|
||||
versionFilePath = path.join(distPath, 'latest-mac.yml')
|
||||
} else {
|
||||
versionFilePath = path.join(distPath, 'latest-linux.yml')
|
||||
console.log('\nUploading version file to root...')
|
||||
await uploadVersionFileToRoot.done()
|
||||
console.log(`${versionFileName} uploaded!`)
|
||||
versionFileStream.close()
|
||||
const versionFileStream2 = fs.createReadStream(versionFilePath)
|
||||
const uploadVersionFileToLatest = new Upload.Upload({
|
||||
client,
|
||||
params: {
|
||||
Bucket: BUCKET,
|
||||
Key: `${FILE_PATH}${versionFileName}`,
|
||||
Body: versionFileStream2,
|
||||
ContentType: mime.lookup(versionFileName),
|
||||
Metadata: {
|
||||
description: 'uploaded by PicList'
|
||||
}
|
||||
}
|
||||
removeDupField(versionFilePath)
|
||||
const versionFileBuffer = fs.readFileSync(versionFilePath)
|
||||
await s3
|
||||
.upload({
|
||||
Bucket: BUCKET,
|
||||
Key: `${versionFileName}`,
|
||||
Body: versionFileBuffer
|
||||
})
|
||||
.promise()
|
||||
await s3
|
||||
.upload({
|
||||
Bucket: BUCKET,
|
||||
Key: `${FILE_PATH}${versionFileName}`,
|
||||
Body: versionFileBuffer
|
||||
})
|
||||
.promise()
|
||||
versionFileHasUploaded = true
|
||||
}
|
||||
})
|
||||
console.log('\nUploading version file to latest...')
|
||||
await uploadVersionFileToLatest.done()
|
||||
console.log(`${versionFileName} uploaded!`)
|
||||
versionFileStream2.close()
|
||||
versionFileHasUploaded = true
|
||||
}
|
||||
} else {
|
||||
console.warn('platform not supported!', platform)
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err)
|
||||
|
||||
Reference in New Issue
Block a user