Skip to content

Instantly share code, notes, and snippets.

@tunchamroeun
Created November 28, 2020 08:44
Show Gist options
  • Select an option

  • Save tunchamroeun/41d0af5dc00a1e84b5508e10b383b6e5 to your computer and use it in GitHub Desktop.

Select an option

Save tunchamroeun/41d0af5dc00a1e84b5508e10b383b6e5 to your computer and use it in GitHub Desktop.
import * as AWS from 'aws-sdk'
const s3 = new AWS.S3({
apiVersion: "latest",
accessKeyId: '',
secretAccessKey: ''
});
async create(create_input: CreateUploadInput): Promise<UploadType | ErrorHandlingMessage> {
try {
//server
/*for (const file of create_input.files) {
//@ts-ignore
const {createReadStream, filename, mimetype} = await file
const fileStream = createReadStream()
fileStream.pipe(fs.createWriteStream(`uploadedFiles/${shortid.generate()}-${filename}`))
}*/
const files = []
//s3
for (const file of create_input.files) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const {createReadStream, filename} = await file
const fileStream = createReadStream()
const uploadParams = {Bucket: 'pks.edu.kh-dev', Key: filename, Body: fileStream};
const result = await s3.upload(uploadParams).promise()
files.push(result.Location)
}
return {
message: 'ជោគជ័យ',
success: true,
files: files
}
} catch (e) {
return {
message: e.message.split(':')[0],
success: false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment