mcohen01 / amazonica

A comprehensive Clojure client for the entire Amazon AWS api.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

No such file or directory exception in AWS Lambda java function

unkownara opened this issue · comments

commented

I am currently making a simple react native application. So I picked some images from gallery and upload into S3 buckets by using AWS Lambda function(Java). But whenever I try to upload those images into s3 bucket I keep getting No such file or directory exception.

`
package com.amazonaws.lambda.demo;
import java.io.File;
import java.io.IOException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.SdkClientException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.PutObjectResult;

public class LambdaFunctionHandler implements RequestHandler<String, PutObjectResult> {
@SuppressWarnings("deprecation")
public PutObjectResult UploadFile(String imageURI) throws IOException {

PutObjectResult resultObj = new PutObjectResult();
AWSCredentials credentials = new BasicAWSCredentials("Ak", "Sk");
//String clientRegion = "region";
String bucketName = "bucket_name";
String fileObjKeyName = "sample_image_pic.jpg";

try {
    AmazonS3 s3Client = new AmazonS3Client(credentials);

    // Upload a file as a new object with ContentType and title specified.

    resultObj = s3Client.putObject(new PutObjectRequest(bucketName, fileObjKeyName, new File(imageURI)));
}
catch(AmazonServiceException e) {
    // The call was transmitted successfully, but Amazon S3 couldn't process 
    // it, so it returned an error response.
    e.printStackTrace();
}
catch(SdkClientException e) {
    // Amazon S3 couldn't be contacted for a response, or the client
    // couldn't parse the response from Amazon S3.
    e.printStackTrace();
}

return resultObj;

}

@OverRide
public PutObjectResult handleRequest(String imageURI, Context context) {

PutObjectResult result = null;
try {
    result = UploadFile(imageURI);
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
return result;

}

}
`

Error Message

{ "contentMd5": "1B2M2Y8AsgTpgAmY7PhCfg==", "metadata": { "userMetadata": {}, "contentLength": 0, "requesterCharged": false, "ETag": "d41d8cd98f00b204e9800998ecf8427e", "rawMetadata": { "Content-Length": 0, "ETag": "d41d8cd98f00b204e9800998ecf8427e" }, "instanceLength": 0 }, "requesterCharged": false, "ETag": "d41d8cd98f00b204e9800998ecf8427e" }

Lambda function input URL -> http://media.santabanta.com/gallery/cricket/ab%20de%20villiers/ab-de-villiers-3-d.jpg

And also I am quite new for these concepts, If anything I made wrong please correct me.

Thanks,