Skip to content

AWS upload issue #2168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
NuonDara opened this issue Jul 28, 2018 · 2 comments
Closed

AWS upload issue #2168

NuonDara opened this issue Jul 28, 2018 · 2 comments
Labels
guidance Question that needs advice or information.

Comments

@NuonDara
Copy link

NuonDara commented Jul 28, 2018

Hi guys,
I am trying upload an file to s3, but shows me an error "Unable to store object contents to disk".
And the file is existed on S3 already.
The file's size is 50MB.
What is wrong?
Thanks.

'use strict';

var AWS = require('aws-sdk');
var AWSCred = require('aws-cred');
var env = process.env.NODE_ENV || "development";
const Env = use('Env');
const trim = use('trim');
const dateFormat = use('dateformat');

var baseBucket = Env.get('AWS_BASE_BUCKET');
var awscred = new AWSCred({"env": env});

var watermarkFolder = Env.get('AWS_WATERMARK_FOLDER');

var s3Client = new AWS.S3({
accessKeyId: awscred.access_key,
secretAccessKey: awscred.secret_access_key,
region: Env.get('AWS_REGION'),
signatureVersion: Env.get('AWS_SIGNATURE_VERSION')
});

var s3RegionalClients = {
"artistarea.gallereplay.com": new AWS.S3({
accessKeyId: awscred.access_key,
secretAccessKey: awscred.secret_access_key,
region: "eu-central-1",
signatureVersion: Env.get('AWS_SIGNATURE_VERSION')
})
};

s3RegionalClients[Env.get('AWS_BASE_BUCKET')] = s3Client;

exports.uploadFormFileStreamToAWS = function * (folderName, fileName, fileStream, options) {

try {
    return yield uploadFormFileStreamToAWSYield(folderName, fileName, fileStream, options);
} catch (e) {
    console.log('Error while uploading file to AWS:', e);
    throw e;
}
};

exports.createBucketIfNotExists = function * () {
try {
console.log("Accessing Bucket with:");
console.log(awscred);
return yield createBucketIfNotExistsYield();
} catch (e) {
console.log('Error while creating or checking bucket on AWS:', e);
throw e;
}
};

exports.getWatermarkUrl = function*(author, pattern, format) {
return yield this.getUrl(watermarkFolder+"/"+format+"/"+author+(pattern ? "_pattern" : "")+".png");
};

exports.getUrl = function(key) {
return function (fn) {
let params = {Bucket: baseBucket, Key: key};
s3Client.getSignedUrl('getObject', params, function (err, url) {
err ? fn(err, null) : fn(null, url);
});
};
};

exports.convertToSignedUrl = function(url, options, output) {
return function (fn) {
var split = url.split("/");
var bucket = split[3];
var pre = split[0] + '/' + split[1] + '/' + split[2] + '/' + split[3] + '/';
var path = url.replace(pre, "");
let params = {Bucket: bucket, Key: path};
if(output) {
output.bucket = bucket;
output.path = path;
}

    var client = s3RegionalClients[bucket];
    client.getSignedUrl('getObject', params, function (err, url) {
        err ? fn(err, null) : fn(null, url);
    });
};
};

exports.getSignedUrl = function (userId, url) {
return function (fn) {
let key = url.substring(url.lastIndexOf('/') + 1, url.length);

    //key = 'user_' + userId + '/' + key.replace(/%20/g, '+');

    key = buildKeyPath('user_' + userId, key);

    let params = {Bucket: baseBucket, Key: key};
    s3Client.getSignedUrl('getObject', params, function (err, url) {
        err ? fn(err, null) : fn(null, url);
    });
};
};

exports.addPreviewUrlToEntityCollection = function * (entityCollection) {
if (entityCollection != null && entityCollection.length && entityCollection[0] && entityCollection[0].user_id) {
for (var i = 0; i < entityCollection.length; i++) {
entityCollection[i].previewUrl = yield this.getSignedUrl(entityCollection[i].user_id, entityCollection[i].upload_path);
}
return entityCollection;
}
else {
return [];
}
};

exports.parseAwsInputKey = function(url) {
var result = url.replace(/http[s]*://.+?//g, "");

if(result.startsWith(baseBucket)) {
    result = result.replace(baseBucket+'/',"");
}
return result;
};

exports.buildUniqueAWSFileName = function (originalTitle, extension) {
// taking the extension out to add it at the end, after inserting a unique timestamp to the filename
return trim(originalTitle.substring(0, originalTitle.lastIndexOf('.')) + '_' + dateFormat(new Date(), "ddmmyyyyhMMss") + '.' + extension).replace(/ /g, '');
};

function buildKeyPath(folderName, key) {
return env + '/' + folderName + '/' + key;
}

function createBucketIfNotExistsYield() {
return function (fn) {
s3Client.headBucket({Bucket: baseBucket}, function (err, data) {
if (err) {
return fn(err, null);
}
//bucket exists
if (data) {
return fn(null, null);
}
else {
s3Client.createBucket({Bucket: baseBucket}, function (err, data) {
err ? fn(err, null) : fn(null, data);
});
}
});

};
}

function uploadFormFileStreamToAWSYield(folderName, fileName, fileStream, options) {

options = options || {};

var baseOptions = {
    Bucket: baseBucket,
    Key: options.isRootPath ? (folderName + '/' + fileName) : buildKeyPath(folderName, fileName),
    Body: fileStream
};

if(options.isRootPath != null) {
    delete options.isRootPath;
}

var allOptions = Object.assign(baseOptions, options);

return function (fn) {
    s3Client.upload(allOptions).send(function (err, data) {
        err ? fn(err, null) : fn(null, data);
    });
}
}
@AllanZhengYP
Copy link
Contributor

AllanZhengYP commented Aug 3, 2018

Hi @NuonDara
Can you provide the stack trace? The error message doesn't look like coming from our SDK for S3. I'm looking at the aws/aws-sdk-java#1288. It looks like the connection is closed by S3, where does the filestream come from. S3 will close inactive connection after the timeout.

@srchase srchase added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Aug 13, 2018
@srchase srchase removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Aug 21, 2018
@srchase srchase closed this as completed Aug 21, 2018
@srchase srchase added guidance Question that needs advice or information. and removed Question labels Jan 4, 2019
@lock
Copy link

lock bot commented Sep 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

@lock lock bot locked as resolved and limited conversation to collaborators Sep 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants