streaming-s3 is not working on live -Nodejs - angularjs

I am using streaming-s3 node-modules, which is working fine on my local machine.
But on live it doesn't seem working. I have https enabled on live server.
if i disabled https on live server my uploading is working fine .
Here is my code
exports.uploadFile = function (fileReadStream, awsHeader, cb) {
//set options for the streaming module
var options = {
concurrentParts: 2,
waitTime: 20000,
retries: 2,
maxPartSize: 10 * 1024 * 1024
};
//call stream function to upload the file to s3
var uploader = new streamingS3(fileReadStream, config.aws.accessKey, config.aws.secretKey, awsHeader, options);
//start uploading
uploader.begin();// important if callback not provided.
// handle these functions
uploader.on('data', function (bytesRead) {
console.log(bytesRead, ' bytes read.');
});
uploader.on('part', function (number) {
console.log('Part ', number, ' uploaded.');
});
// All parts uploaded, but upload not yet acknowledged.
uploader.on('uploaded', function (stats) {
console.log('Upload stats: ', stats);
});
uploader.on('finished', function (response, stats) {
console.log(response);
logger.log('info', "UPLOAD ", response);
cb(null, response);
});
uploader.on('error', function (err) {
console.log('Upload error: ', err);
logger.log('error', "UPLOAD Error: ", err);
cb(err);
});
Any idea about this
Thanks

You need to enable ssl in your client, add sslEnabled option like following
var options = {
sslEnabled: true,
concurrentParts: 2,
waitTime: 20000,
retries: 2,
maxPartSize: 10 * 1024 * 1024
};
you can check more about the options you can use http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html

Related

evaporate.js s3 direct uploads 403

I am attempting to upload files directly to the browser to S3 using evaporate js.
I followed the tutorial at jqueryajaxphp.com but I am having a problem with the signature
Signature.php
<?php
$to_sign = $_GET['to_sign'];
$secret = 'AWS_SECRET';
$hmac_sha1 = hash_hmac('sha1', $to_sign, $secret, true);
$signature = base64_encode($hmac_sha1);
echo $signature;
Upload function
function largeFileUPload(file) {
var ins = new Evaporate({
signerUrl: './includes/s3-signature.php',
aws_key: "AWS_KEY_XXXXXXX",
bucket: 'bucket-name',
awsRegion: 'eu-west-1',
cloudfront: true,
aws_url: 'http://bucket-name.s3-accelerate.amazonaws.com',
// partSize: 10 * 1024 * 1024,
s3Acceleration: true,
computeContentMd5: true,
cryptoMd5Method: function (data) { return AWS.util.crypto.md5(data, 'base64'); },
cryptoHexEncodedHash256: function (data) { return AWS.util.crypto.sha256(data, 'hex'); }
});
// http://<?=$my_bucket?>.s3-<?=$region?>.amazonaws.com
ins.add({
name: 'evaporateTest' + Math.floor(1000000000*Math.random()) + '.' + file.name.replace(/^.*\./, ''),
file: file,
xAmzHeadersAtInitiate : {
'x-amz-acl': 'public-read'
},
signParams: {
foo: 'bar'
},
complete: function(r){
console.log('Upload complete');
},
progress: function(progress){
var progress = Math.floor(progress*100);
console.log(progress);
},
error: function(msg){
console.log(msg);
}
});
}
I am getting to following response from from the s3 end point
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
I have also tried the blueimp plugin but this fails with files over 200MB
The Signature.php file form the tutorial from jqueryajaxphp.com was causing the problem. I altered the php signature file form the evaporate.js docs and it solved the problem
Signature.php
$to_sign = $_GET['to_sign'];
$secret = 'AWS_SECRET';
$formattedDate = substr($dateTime, 0, 8);
//make the Signature, notice that we use env for saving AWS keys and regions
$kSecret = "AWS4" . $secret;
$kDate = hash_hmac("sha256", $formattedDate, $kSecret, true);
$kRegion = hash_hmac("sha256", "eu-west-1", $kDate, true);
$kService = hash_hmac("sha256", 's3', $kRegion, true);
$kSigning = hash_hmac("sha256", "aws4_request", $kService, true);
$signature = hash_hmac("sha256", $to_sign, $kSigning);
echo $signature;

upload dynamically generated pdf from aws-lambda into aws-s3

In my serverless app, I want to create pdf which is generated dynamically and then upload that created pdf into aws s3. My problem is, when a url is returned to client-side code from server, uploaded url doesn't working. My code is given below:
Client-side javascript code (angular.js)
$scope.downloadAsPDF = function() {
// first I have to sent all html data into server
var html = angular.element('html').html(); // get all page data
var service = API.getService();
service.downloadPdf({}, { html : html }, // api call with html data
function(res) {
console.log("res : ", res);
window.open(res.url); // open uploaded pdf file
// err: The server replies that you don't have permissions to download this file
// HTTP/1.1 403 Forbidden
}, function(err) {
console.log("err : ", err);
});
};
Serverless Code
var fs = require('fs');
var pdf = require('html-pdf');
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.handler = function(event, context) {
if (event.html) { // client html data
AWS.config.update({
accessKeyId: 'xxxxxxxxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
region: 'xxxxxxxxxxxxxxxxxxxx'
});
var awsInfo = {
bucket: 'xxxxx-xxxxxx'
};
var baseUrl = 'https://s3-my-region.amazonaws.com/s3-upload-directory';
var folderRoot = 'development/pdf';
// unique file name
var output_filename = Math.random().toString(36).slice(2) + '.pdf';
// file created directory
var output = '/tmp/' + output_filename;
pdf.create(event.html, options).toStream(function(err, stream) {
if( err ) {
console.log('pdf err : ', err);
} else {
writeStream =fs.createWriteStream(output);
s3.putObject({
Bucket : awsInfo.bucket,
Key : folderRoot + '/' + output_filename,
Body : fs.createReadStream(output),
ContentType : "application/pdf"
},
function(error, data) {
if (error != null) {
console.log("error: " + error);
} else {
// upload data: { ETag: '"d41d8cd98f00b204e9800998ecf8427e"' }
console.log('upload data : ', data);
return cb(null, {
// return actual aws link, but no file
// ex: 'https://s3-my-region.amazonaws.com/s3-upload-directory/output_filename.pdf
url: baseUrl + '/' + output_filename
});
}
});
}
}
};
I've solve my problem. I was trying to upload pdf before I generate pdf. I have solve this problem using the following code:
pdf.create(event.html, options).toStream(function(err, stream) {
if (err) {
console.log('pdf err : ', err);
} else {
var stream = stream.pipe(fs.createWriteStream(output));
stream.on('finish', function () {
s3.putObject({
Bucket : awsInfo.bucket,
Key : folderRoot + '/' + output_filename,
Body : fs.createReadStream(output),
ContentType : "application/pdf"
},
function(error, data) {
if (error != null) {
console.log("error: " + error);
return cb(null, {
err: error
});
} else {
var url = baseUrl + '/' + output_filename
return cb(null, {
url: url
});
}
});
});
}
});
I have done similar kind of thing before. I want a few clarifications from you and then I will be able to help you better.
1) In your code (server side), you have mentioned in the callback function that actual aws link is getting returned.
Are you sure that your file is getting uploaded to Amazon s3. I mean did you check your bucket for the file or not?
2) Have you set any custom bucket policy on Amazon s3. Bucket policy play an important role in what can be downloaded from S3.
3) Did you check the logs to see exactly which part of code is causing the error?
Please provide me this information and I think the I should be able to help you.
if we don't want to upload at s3 just return generated file from aws-lambda.

Cordova screenshot not working on time $IonicView.Enter

We are working on an iOS project in Ionic. We want the cordova screenshot plugin to fire on ionicview enter (when first entering the app), and then use cordova file transfer to send the screenshot.
The below code does not work when first time entering the view. When we leave the view and come back however, it does send a screenshot.
However, the first logAction DOES fire on the first time entering this view while the second logAction does not. When entering this view for the second time, both logActions are fired.
This is the part of the code i am referring to:
$scope.$on('$ionicView.enter', function () {
$scope.logAction({
"Message": "Login screen succesfully entered",
"Succeeded": true,
"transaction": 1,
"Category": "Info",
"Device": 0,
})
$cordovaScreenshot.capture().then(function(filepath){
$scope.logAction({
"Message": filepath,
"Succeeded": true,
"transaction": 1,
"Category": "Info",
"Device": 0,
})
$cordovaScreenshot.send(filepath);
});
});
This is the cordovaScreenshot file
angular.module('testScreenshots.services', [])
.service('$cordovaScreenshot', ['$q',function ($q) {
return {
fileURL: "",
capture: function (filename, extension, quality) {
var randomNumber = Math.random();
console.log("" + randomNumber);
filename = "testPicture" + randomNumber.toString();
extension = extension || 'jpg';
quality = quality || '100';
var defer = $q.defer();
navigator.screenshot.save(function (error, res){
if (error) {
console.error(error);
defer.reject(error);
} else {
console.log('screenshot saved in: ', res.filePath);
this.fileURL = "file://" + res.filePath;
defer.resolve(res.filePath);
console.log("inside the save function: "+this.fileURL);
}
}, extension, quality, filename);
return defer.promise;
},
send: function(filepath){
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = filepath.substr(filepath.lastIndexOf('/') + 1);
options.mimeType = "multipart/form-data";
options.chunkedMode = false;
options.headers = {
Connection: "close"
};
var ft = new FileTransfer();
ft.upload(filepath, encodeURI("http://192.168.6.165:8080//api/uploadpicture"), win, fail, options);
}
};
}])
The iOS simulator we use on the Mac had this issue. After trying to run it on a device the issue no longer affected us.
This issue thus seems to relate to using the iOS simulator.

Node.js proxy file uploading

I have a MEAN.js application in which I use https://github.com/nervgh/angular-file-upload to handle angular file uploads to server. What I want to do is receive the files on the server, and then pipe them to another server where I would like to use a writeStream to write them.
Is there a way I can do this?
ar data = {
file : {
buffer:req.files.file.buffer,
filename : req.files.file.name,
content_type : 'application/octet-stream'
},
options : {
operation : 'Content',
source : req.body.source
}
};
needle.post(config.vdnURL,data,{ multipart:true},function(error, response) {
on the other server which is supposed to receive the file and write it to disk, I do the following
mkdirp(config.winUploadPath+ req.body.options.source + '/'+ req.body.options.operation +'/', function(err) {
if (err) {
console.log(err);
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
fs.writeFile(config.winUploadPath + req.body.options.source + '/'+ req.body.options.operation +'/' + req.files.file.name, req.files.file.buffer, function (uploadError) {
if (uploadError) {
console.log('Got upload error ', uploadError);
return res.status(400).send({
message: 'Error occurred while uploading profile picture'
});
} else {
res.status(200).send({
message: 'Upload successful',
url: req.body.options.source + '/'+ req.body.options.operation +'/' + req.files.file.name
});
}
});
}
});
The problem arises when the file size exceeds a couple of MB's (>3-4MB). Files don't get written at all and the second server shows
POST / -- ms -- with no file written.
Hence I thought its a streaming problem. Any help on how maybe I can solve it will be greatly appreciated.

Phonegap / Cordova - File Download (File plugin) - Windows Phone 8

I am developing an application for a radio station, have a section to download your audio programs. Works perfectly on Android and iOS, but Windows Phone starts downloading (very very slow) and after a time the application breaks showing this error message
An exception of type 'System.Runtime.InteropServices.SEHException' occurred in Unknown Module. and wasn't handled before a managed/native boundary
Also during the short time that is downloading shows this message
DispatchFileTransferProgress : FileTransfer1024600849
The size of the files you download is between 15 and 20 mb.
Anyone know what happens? Thanks in advance
Edited for add code
Code:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onRequestFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory(
'Radio', //Directorory was created when apps starts firs time
{ create: true, exclusive: false },
function(directoryEntry) {
directoryEntry.getFile(
'dummy.html',
{ create: true, exclusive: false },
function onGetFileSuccess(fileEntry) {
var path = fileEntry.toURL().replace('dummy.html', '');
var fileTransfer = new FileTransfer();
fileEntry.remove();
var url = record.get('file');
var file = record.get('file').split('/');
fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
console.log('Downloaded: ' + ((progressEvent.loaded / progressEvent.total) * 100));
}
};
fileTransfer.download(
url,
path + file[file.length - 1],
function(file) {
console.log('Downloaded!');
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
},
function fail(error) {
console.log('Get file ' + error.code);
}
);
},
function fail(error) {
console.log('Get directory ' + error.code);
}
);
},
function fail(error) {
console.log('Get file system ' + error.code);
}
);
Miguel

Resources