I'm trying to do a server-less upload to S3 using DropzoneJS. I'm having issues with the AWS Presigned URL specifically, where it's indicating that the x-amz-acl header is unsigned.
Javascript:
var dz = new Dropzone("div#upload", {
url: "tbd",
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: this.getUploadUrl,
method: 'put',
sending: function(file, xhr) {
var _send = xhr.send;
xhr.setRequestHeader('x-amz-acl', 'public-read');
xhr.send = function() {
_send.call(xhr, file);
}
},
});
dz.on('processing', function(file) {
// change url before sending
this.options.url = file.uploadUrl;
});
function getUploadUrl(file, cb) {
var params = {
fileName: file.name,
fileType: file.type,
};
$.getJSON('signput.php', params).done(function(data) {
var decodedUri = decodeURIComponent(data['signedRequest']);
if (!data.signedRequest) {
return cb('Failed to receive an upload url');
}
console.log(decodedUri);
file.uploadUrl = decodedUri;
cb();
}).fail(function() {
return cb('Failed to receive an upload url');
});
}
PHP (called to get presigned url):
$fileName = $_GET['fileName'];
$s3Client = new Aws\S3\S3Client([
'version' => '2006-03-01',
'region' => 'us-west-2',
'credentials' => [
'key' => '__MY__KEY__',
'secret' => '__MY__SECRET__',
],]);
$cmd = $s3Client->getCommand('PutObject', [
'Bucket' => '__MY__BUCKET__',
'Key' => $fileName
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$url = (string) $request->getUri();
$urlArray['signedRequest'] = $url;
$urlArray = json_encode($urlArray);
echo $urlArray;
I've also tried setting x-amz-acl to public-read in the Dropzone headers and S3 getCommand, but it's not working.
The error I get:
<?xml version="1.0" encoding="UTF-8"?>
<Error><Code>AccessDenied</Code>
<Message>There were headers present in the request which were not signed</Message>
<HeadersNotSigned>x-amz-acl</HeadersNotSigned>
</Error>
There was one issue - I needed to move the ACL => 'public-read' from the JS code into the signing request.
The Dropzone sending function turns into this:
sending: function(file, xhr) {
var _send = xhr.send;
xhr.send = function() {
_send.call(xhr, file);
}
}
And the PHP signing requests turns into:
$cmd = $s3Client->getCommand('PutObject', [
'Bucket' => '__MY__BUCKET__',
'Key' => $fileName,
'ACL' => 'public-read'
]);
Thanks to Michael for pointing me in the right direction.
Final code for reference...
Javascript:
var dz = new Dropzone("div#upload", {
url: "tbd",
paramName: "file", // The name that will be used to transfer the file
maxFilesize: 2, // MB
accept: this.getUploadUrl,
method: 'put',
sending: function(file, xhr) {
var _send = xhr.send;
xhr.send = function() {
_send.call(xhr, file);
}
},
});
dz.on('processing', function(file) {
// change url before sending
this.options.url = file.uploadUrl;
});
function getUploadUrl(file, cb) {
var params = {
fileName: file.name,
fileType: file.type,
};
$.getJSON('signput.php', params).done(function(data) {
var decodedUri = decodeURIComponent(data['signedRequest']);
if (!data.signedRequest) {
return cb('Failed to receive an upload url');
}
file.uploadUrl = decodedUri;
cb();
}).fail(function() {
return cb('Failed to receive an upload url');
});
}
PHP:
$fileName = $_GET['fileName'];
$s3Client = new Aws\S3\S3Client([
'version' => '2006-03-01',
'region' => 'us-west-2',
'credentials' => [
'key' => '__MY_KEY__',
'secret' => '__MY_SECRET__,
],]);
$cmd = $s3Client->getCommand('PutObject', [
'Bucket' => '__MY_BUCKET__',
'Key' => $fileName,
'ACL' => 'public-read'
]);
$request = $s3Client->createPresignedRequest($cmd, '+20 minutes');
// Get the actual presigned-url
$url = (string) $request->getUri();
$urlArray['signedRequest'] = $url;
$urlArray = json_encode($urlArray);
echo $urlArray;
Related
I developed a PHP project, now I am working on connecting it to react native app. However, I tried many codes to upload image to the server nothing works.
Here is my code example,
const uploadImage = async () => {
// Check if any file is selected or not
if (singleFile != null) {
// If file selected then create FormData
const fileToUpload = singleFile;
const data = new FormData();
data.append('file_attachment', fileToUpload);
// Please change file upload URL
fetch(
'http://192.168.8.105/insaf/mobileConnection/upload.php',
{
method: 'post',
body: data,
headers: {
'Content-Type': 'multipart/form-data; ',
},
}
).then((response) => response.json())
.then((responseJson) => {
//Hide Loader
setLoading(false);
console.log(responseJson);
// If server response message same as Data Matched
if (responseJson[0].Message == "Success") {
navigation.replace('ReqPriceList');
} else {
//setErrortext(responseJson.msg);
console.log('Please check');
}
})
.catch((error) => {
//Hide Loader
setLoading(false);
console.error(error);
});
} else {
// If no file selected the show alert
alert('Please Select File first');
}
};
And for the PHP server side (upload.php), here is the code
if(!empty($_FILES['file_attachment']['name']))
{
$target_dir = "../assets/files/request/";
if (!file_exists($target_dir))
{
mkdir($target_dir, 0777);
}
$target_file =
$target_dir . basename($_FILES["file_attachment"]["name"]);
$imageFileType =
strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
echo json_encode(
array(
"status" => 0,
"data" => array()
,"msg" => "Sorry, file already exists."
)
);
die();
}
// Check file size
if ($_FILES["file_attachment"]["size"] > 50000000) {
echo json_encode(
array(
"status" => 0,
"data" => array(),
"msg" => "Sorry, your file is too large."
)
);
die();
}
if (
move_uploaded_file(
$_FILES["file_attachment"]["tmp_name"], $target_file
)
) {
echo json_encode(
array(
"status" => 1,
"data" => array(),
"msg" => "The file " .
basename( $_FILES["file_attachment"]["name"]) .
" has been uploaded."));
} else {
echo json_encode(
array(
"status" => 0,
"data" => array(),
"msg" => "Sorry, there was an error uploading your file."
)
);
}
}
I got this code from here https://aboutreact.com/file-uploading-in-react-native/
And I am getting
this error
Can anyone help me?
Any alternative solution will be fine.
Edit:
based on #Sadia Chaudhary code this function works
let uploadImage = async () => {
//Check if any file is selected or not
if (singleFile != null) {
//If file selected then create FormData
const fileToUpload = singleFile;
console.log("fileToUpload is " + fileToUpload);
const uriPart = fileToUpload[0].uri;
const fileExtension = fileToUpload[0].name.split('.')[1];
console.log("fileExtension is " + fileExtension);
const data = new FormData();
//const uriPart = fileToUpload.split('.');
//const fileExtension = uriPart[uriPart.length - 1];
data.append('file_attachment', {
uri: uriPart,
name: `photo.${fileExtension}`,
type: `image/${fileExtension}`
});
let res = await fetch(
'http://myIp/insaf/mobileConnection/uploads.php',
{
method: 'post',
body: data,
headers: {
'Content-Type': 'multipart/form-data; ',
},
}
);
let responseJson = await res.json();
if (responseJson.status == 1) {
alert('Upload Successful');
}
} else {
//if no file selected the show alert
alert('Please Select File first');
}
};
The error has nothing to do with the image upload. You are facing this error because of response.json(). Also, try to convert image like this.
//retrive the file extension of your photo uri
const uriPart = imageSource.split('.');
const fileExtension = uriPart[uriPart.length - 1];
formData.append('photo', {
uri: imageSource,
name: `photo.${fileExtension}`,
type: `image/${fileExtension}`
});
we created an ionic app which captures video but we are facing challenges saving this video to a directory in on the server. I have searched online for this but couldn't find a tutorial about it.
On the code below, the video name was saved in the videos folder instead of the actual video
//Upload video php part
$postjson = json_decode(file_get_contents('php://input'), true);
if(!empty($postjson['video'])){
$video = $postjson['video'];
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now -> format('YmdHisu');
$upload_folder = "videos";
$path ="$upload_folder/$id.mp4";
$actualpath = "http://url/incident/api/$path";
file_put_contents($path,$video);
}
On the ionic part, I have this:
captureVideo() {
let options: CaptureVideoOptions = {
limit: 1,
duration: 120
}
this.mediaCapture.captureVideo(options).then((res: MediaFile[]) => {
let capturedFile = res[0];
let fileName = capturedFile.name;
let dir = capturedFile['localURL'].split('/');
dir.pop();
let fromDirectory = dir.join('/');
var toDirectory = this.file.dataDirectory;
this.ivideo = fileName;
this.viddir = dir;
console.log("video captured "+this.ivideo);
this.file.copyFile(fromDirectory , fileName , toDirectory , fileName).then((res) => {
this.storeMediaFiles([{name: fileName, size: capturedFile.size}]);
},err => {
console.log('err: ', err);
});
},
(err: CaptureError) => console.error(err));
}
Sending to the server using post method in code summary:
var headers = new Headers();
headers.append('Content-Type', 'application/json' );
//headers.append('Access-Control-Allow-Methods', 'POST' );
headers.append('Content-Type', 'multipart/form-data');
let options = new RequestOptions({ headers: headers });
let data = {
aksi : 'insert_incident',
title: this.title.value,
descr: this.descr.value,
images : this.cameraData,
video: this.ivideo,
};
this.http.post(this.global.serverAddress+"api/getCategories.php", data, options)
.map(res => res.json())
.subscribe(res => {
loader.dismiss()
if(res=="Done"){
let alert = this.alertCtrl.create({
title:"Done",
subTitle: "Sent",
buttons: ['OK']
});
alert.present();
}else
{
let alert = this.alertCtrl.create({
title:"ERROR",
subTitle:(res),
buttons: ['OK']
});
alert.present();
}
}
I have code that sends an image to my server and everything works fine. However i am now truing to send a file to my server using an input tag in ionic However i cant seem to get it working.
I get an error from the php file saying undefined index 'file'
HTML
<ion-col>
<ion-label>Add a File</ion-label>
<ion-input type='file' [(ngModel)]="fileName"></ion-input>
</ion-col>
TS file
addFile() {
this.addService.addFile(this.fileName).subscribe(res => {
console.log(res);
});
}
service
addFile(file) {
let headers = new HttpHeaders();
// headers = headers.set('Content-Type', 'application/json');
headers = headers.set('Authorization', '' + this.token);
const formData = new FormData();
formData.append('file', file);
return this.http.post<any>('http://domain.fileUpload.php', formData, {headers});
}
PHP API
$target_path = "files/";
$target_path = $target_path . basename( $_FILES['file']['name']);
$findDate = date("Y-m-d");
if (move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
header('Content-type: application/json');
$data = ['success' => true, 'message' => 'Upload and move success'];
echo json_encode( $data );
} else {
header('Content-type: application/json');
$data = ['success' => false, 'message' => 'There was an error uploading the file (folder), please try again!'];
echo json_encode( $data );
}
uploadMethod
uploadFile: ''; // from ngModel
fileUpload(path) {
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'file',
fileName: '.png',
chunkedMode: false,
};
console.log(this.uploadFile);
fileTransfer
.upload(this.uploadFile, 'http://domain/fileUpload.php',
options
)
.then(
data => {
console.log(data + 'Uploaded Successfully');
// console.log(JSON.parse(data.));
// let res = JSON.parse(data);
let res = data;
if (res['success']) {
console.log('True');
}
},
err => {
console.log(err);
}
);
}
Blow is the Example Code:
import { Platform } from 'ionic-angular';
checkPlatform: boolean = fasle;
constructor(public plt: Platform) {
if (this.plt.is('ios')) {
this.checkPlatform == true; // if platform ios this will be true
}
if (this.plt.is('android')) {
this.checkPlatform == true; // if platform androidthis will be true
}
}
imageUpload(path) {
const fileTransfer: FileTransferObject = this.transfer.create();
let options: FileUploadOptions = {
fileKey: 'image',
fileName: '.png',
chunkedMode: false,
//mimeType: "image/jpeg",
}
fileTransfer.upload(path, 'https://yourDomain.com/api/imageUpload', options)
.then((data) => {
console.log(data+" Uploaded Successfully");
console.log(JSON.parse(data.response));
let res = JSON.parse(data.response);
if (res.success == true) {
// do whats ever you want to do
}
}, (err) => {
console.log(err);
});
}
Pass the cordova file path as parameter in this function.
inside you HTML template show buttons or input type like this:
<input type="file" *ngIf="checkPlatform == true">
if you see this you can notice allowed types are :
export type TextFieldTypes = 'date' | 'email' | 'number' | 'password' | 'search' | 'tel' | 'text' | 'url' | 'time';
if you want to upload files follow this link i think you will find the answer
I check your code and as i see file added to FormData is filename with string Type, not File or Blob Data Type. So in this case it will not send file into $_FILES, it actually send its value to $_POST in your PHP Server. In summary, File and Blob Type will be sent to $_FILES, other data types will be sent to the appropriate global variables.
There is also a good example on how to validate your file in PHP Server: https://www.php.net/manual/en/features.file-upload.php
Im following some tutorial to upload file on server by php api. I need to send some data with file. Like with file i want to send a name. So it will generate folder by that name and save file in that folder.
home.ts
onFileSelect(event) {
if (event.target.files.length > 0) {
const file = event.target.files[0];
this.form.get('avatar').setValue(file);
}
}
onSubmit() {
console.log(this.backUrl);
name = this.backUrl;
console.log(name);
const formData = new FormData();
formData.append('avatar', this.form.get('avatar').value);
this.uploadService.uploadFile(formData).subscribe(
(res) => {
this.uploadResponse = res;
console.log(res);
},
(err) => {
console.log(err);
}
);
}
service.ts
public uploadFile(data,) {
let uploadURL = 'http://api.igiinsurance.com.pk:8888/file_upload/upload.php';
return this.httpClient.post<any>(uploadURL, data);
}
upload.php
<?php
header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: PUT, GET, POST");
$response = array();
$upload_dir = 'uploads';
$server_url = 'http://api.igiinsurance.com.pk:8888';
if($_FILES['avatar'])
{
$avatar_name = $_FILES["avatar"]["name"];
$avatar_tmp_name = $_FILES["avatar"]["tmp_name"];
$error = $_FILES["avatar"]["error"];
if($error > 0){
$response = array(
"status" => "error",
"error" => true,
"message" => "Error uploading the file!"
);
}else
{
$random_name = rand(1000,1000000)."-".$avatar_name;
$upload_name = $upload_dir.strtolower($random_name);
$upload_name = preg_replace('/\s+/', '-', $upload_name);
if(move_uploaded_file($avatar_tmp_name , $upload_name)) {
mkdir("upload/testing3");
$response = array(
"status" => "success",
"error" => false,
"message" => "File uploaded successfully",
"url" => $server_url."/".$upload_name
);
}else
{
$response = array(
"status" => "error",
"error" => true,
"message" => "Error uploading the file!"
);
}
}
}else{
$response = array(
"status" => "error",
"error" => true,
"message" => "No file was sent!"
);
}
echo json_encode($response);
?>
I want to send name with file. And in php that name will be the folder of that file. Example All files are now saving in ""uploads"" folder. I need file save in upload>name>file-here. Any one can help in code so please
handleInputChange(event: any) {
const image = event.target.files[0];
const reader = new FileReader();
if (!image.type.match(/image-*/)) {
this.Toastr.error('Image are not valid.', 'Failed!');
return;
} else {
this.photo = image;
reader.onload = (event1: ProgressEvent) => {
this.url = (<FileReader>event1.target).result;
};
reader.readAsDataURL(event.target.files[0]);
this.selectedFile = event.target.files[0];
this.validateImage = false;
}
}
this.ServiceName.saveNewsData("api_url", this.selectedFile).subscribe((response: any) => {
if (response.status == true) {}})
saveNewsData(url, photo) {
const data = new FormData();
data.append('featuredImage', photo);
return this.http.post(url, data,{ headers: header })
.map(response => response)
.catch(this.handleError);
}
I am trying to build an email server for my domains... What I'm doing now is receiving emails through SES and storing them in a S3 bucket, then when a user access the inbox it fetches the new emails and store them in my EC2 instance database.
Though it works I'm not completely satisfied with this solution, does anyone know any other/better ways to work this receiving-storing-accessing problem?
Thanks in advance.
You can try to install postfix (http://www.postfix.org/) and dovecot (http://www.dovecot.org/) on your EC2 instance.
I've resolved this and posted the answer on another question I'd made here.
But I'll repost it here anyway:
So what I did was storing the email received in an S3 bucket, than notifying my api that a new email has arrived (sending the file name). Finally read from S3, parsed, stored and deleted from S3, inside my api.
SES rules:
Lambda notifying function:
Note that the name of the S3 file created by the first rule is the same as the messages id, hence 'fileName': event.Records[0].ses.mail.messageId.
'use strict';
exports.handler = (event, context, callback) => {
var http = require('http');
var data = JSON.stringify({
'fileName': event.Records[0].ses.mail.messageId,
});
var options = {
host: 'my.host',
port: '80',
path: '/my/path',
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': data.length
}
};
var req = http.request(options, function(res) {
var msg = '';
res.setEncoding('utf8');
res.on('data', function(chunk) {
msg += chunk;
});
res.on('end', function() {
console.log(JSON.parse(msg));
context.succeed();
});
});
req.write(data);
req.end();
};
Api function (PHP - Laravel):
Note that I'm using an email parser that's based on Plancake Email Parser (link here) with some changes of my own and if needed I'll edit to show the source.
public function process_incoming_email(Request $request)
{
$current_time = Carbon::now()->setTimezone('Brazil/East'); // ALL TIMEZONES: http://us.php.net/manual/en/timezones.others.php
try
{
if ($request->has('fileName')
{
$file_name = $request->input('fileName');
// GET CREDENTIALS AND AUTHENTICATE
$credentials = CredentialProvider::env();
$s3 = new S3Client([
'version' => 'latest',
'region' => 'my-region',
'credentials' => $credentials
]);
// FECTH S3 OBJECT
$object = $s3->GetObject(['Bucket' => 'my-bucket', 'Key' => $file_name]);
$body = $object['Body']->getContents();
// PARSE S3 OBJECT
$parser = new EmailParser($body);
$receivers = ['to' => $parser->getTo(), 'cc' => $parser->getCc()];
$from = $parser->getFrom();
$body_plain = $parser->getPlainBody();
$body_html = $parser->getHTMLBody();
$subject = $parser->getSubject();
$error_message;
// PROCESS EACH RECEIVER
foreach ($receivers as $type => $type_receivers)
{
foreach ($type_receivers as $receiver)
{
// PROCESS DOMAIN-MATCHING RECEIVERS
if(preg_match("/#(.*)/", $receiver['email'], $matches) && $matches[1] == self::HOST)
{
// INSERT NEW EMAIL
$inserted = DB::table('my-emails')->insert([
// ...
]);
}
}
}
// ADD ERROR LOG IF PARSER COULD NOT FIND EMAILS
if($email_count == 0)
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Could not parse received email or find a suitable user receiving email.') . ' File: ' . $file_name]
);
}
// DELETE OBJECT FROM S3 IF INSERTED
else if(count($emails) == $email_count)
{
$s3->deleteObject(['Bucket' => 'my-bucket', 'Key' => $file_name]);
// RETURN SUCCESSFUL JSON RESPONSE
return Response::json(['success' => true, 'receivedAt' => $current_time, 'message' => 'Email successfully received and processed.']);
}
// ADD ERROR LOG IF NOT INSERTED
else
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Inserted ' . count($emails) . ' out of ' . $email_count . ' parsed records.') . ' File: ' . $file_name]
);
}
}
else
{
// ERROR: NO fileName FIELD IN RESPONSE
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'Incorrect request input format.') . ' Input: ' . json_encode($request->all())]
);
}
}
// ERROR TREATMENT
catch(Exception $ex)
{
DB::table('my-logs')->insert(
['sender' => $request->ip(), 'type' => 'error', 'content' => ($error_message = 'An exception occurred while processing an incoming email.') . ' Details: ' . $ex->getMessage()]
);
}
// RETURN FAILURE JSON RESPONSE
return Response::json(['success' => false, 'receivedAt' => $current_time, 'message' => $error_message]);
}