Skip to content

Instantly share code, notes, and snippets.

@squallcs12
Created November 24, 2019 19:05
Show Gist options
  • Save squallcs12/5fc3d0feb5922e303f6479b1e207c9ce to your computer and use it in GitHub Desktop.
Save squallcs12/5fc3d0feb5922e303f6479b1e207c9ce to your computer and use it in GitHub Desktop.
import {call, fork, takeLatest} from 'redux-saga/effects';
import axios from 'axios';
import _ from 'lodash'
import {constants as awsConstants} from '../modules/aws';
export function* uploadFilesToS3 (action) {
const {files, callback, onUploadProgress} = action.payload
try {
const fileParams = [];
for (let i = 0; i < files.length; i++) {
const file = files[i]
fileParams.push({
name: file.name,
type: file.type,
size: file.size,
})
}
const response = yield call(axios.post, CONFIG.apiURL + '/api/common/s3_sign', {
files: fileParams,
})
for (let i = 0; i < files.length; i++) {
const file = files[i]
let uploadData = new FormData()
const signedRequest = response.data.signed_requests[i]
_.forEach(signedRequest.fields, (value, key) => {
uploadData.append(key, value)
})
uploadData.append('ACL', 'public-read')
uploadData.append('file', file)
axios.post(signedRequest.url, uploadData, {
transformRequest: [
(data, headers) => {
delete headers.common['Authorization']
return data
},
],
onUploadProgress: onUploadProgress,
}).then(function (data) {
callback(i, file, signedRequest.url + signedRequest.fields.key, signedRequest.fields.key);
})
}
} catch (e) {
}
}
function* watchSploadFilesToS3 () {
yield takeLatest(awsConstants.UPLOAD_FILES_TO_S3, uploadFilesToS3);
}
export const awsSaga = [
fork(watchSploadFilesToS3),
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment