lundi 12 octobre 2015

Android Amazon S3 file upload not working

I am trying to upload a file on the sd card to my amazon s3 bucket. Here is the class:

public class FileUploadDownload
{
    Context context;
    boolean transferComplete = false;
    float transferPercentage = 0;
    public FileUploadDownload(Context context)
    {
        this.context = context;
    }

    public void uploadFile(File file , String nameOfFile)
    {
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                context,
                "eu-west-1:a7f38780-ed69-4fb1-a31b-fcfec441b205", // Identity Pool ID
                Regions.EU_WEST_1 // Region
        );
        AmazonS3 s3 = new AmazonS3Client(credentialsProvider);
        TransferUtility transferUtility = new TransferUtility(s3, context);
        TransferObserver observer = transferUtility.upload(
                "scurebucket.ireland.recordepisodes",     /* The bucket to upload to */
                nameOfFile,    /* The key for the uploaded object */
                file        /* The file where the data to upload exists */
        );
        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                Toast.makeText(context,"State changed to : "+state.toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

              //  transferPercentage = (float)bytesCurrent/(float)bytesTotal;
                if(bytesCurrent == bytesTotal)
                {
                //    transferComplete = true;
                    Toast.makeText(context,"Upload completed!",Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(int id, Exception ex) {
                Toast.makeText(context,"Upload Unsuccessful due to `"+ex.toString(),Toast.LENGTH_LONG).show();
            }
        });
    }
}

Here is the code snippet which I am implementing in the OnCreate() method of the Activity:

 File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/EpisodeRecorder/User1/Episode1/User1e1p1.jpg");
    FileUploadDownload fud = new FileUploadDownload(this);
    fud.uploadFile(dir,"trialFileUpload.jpg");

Here is the top part of the manifest:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/CustomActionBarTheme" >
    <service
        android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
        android:enabled="true" />
.....

I am showing all the debug messages through toasts. Initially it shows "IN PROGRESS" and after a bit, it says "FAILED" the error message says "java.lang.IllegalStateException" In the DEBUG LOG it says:

10-12 21:01:01.138   1252-14041/? E/CloudSettingsConnection﹕ Connection failed : SERVER_ERROR

I am not able to figure out what's happening. It would be great if you could point out the error. Or I'd be glad if you can figure out whether the error is in the Amazon s3 / cognito / IAM setup or the code. Thanks!




Aucun commentaire:

Enregistrer un commentaire