Created
November 28, 2020 08:44
-
-
Save tunchamroeun/41d0af5dc00a1e84b5508e10b383b6e5 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
| 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