开发者

Uploading an image to Amazon S3

I need to upload a bitmap to Amazon S3. Below is the code I have so far, built after going through the docs and sample code.

public class AmazonS3Test {

private static final String TAG = "MyApp.AmazonS3Stuff";
private static AmazonS3 mS3 = null;
private static final String mS3BucketName = "bucketname";
private static BasicAWSCredentials mCredentials = new BasicAWSCredentials(".....", "....");

private static void uploadImageToAmazonS3(String key, File file) {
    PutObjectRequest request = new PutObjectRequest(mS3BucketName, key, file);
    try {
        PutObjectResult result = getS3Instance().putObject(request);
    } catch (AmazonClientException e) {
        Log.e(TAG, "Amazon exception uploading the image to Amazon S3 " + key + " " + mS3BucketName, e);
    } catch (Exception e) {
     开发者_开发问答   Log.e(TAG, "Exception uploading the image to Amazon S3 " + key + " " + mS3BucketName, e);
    }
    // TODO Handle result
}

private static AmazonS3 getS3Instance() {
    if (mS3 == null) {
        mS3 = new AmazonS3Client(mCredentials);    // <---- Exception here
    }
    return mS3;
}

}

Stack trace:

    Caused by: java.lang.NoSuchMethodError: org.apache.commons.httpclient.params.i.a
    at com.amazonaws.http.HttpClient.<init>(Unknown Source)
    at com.amazonaws.AmazonWebServiceClient.<init>(Unknown Source)
    at com.amazonaws.services.s3.AmazonS3Client.<init>(Unknown Source)
    at com.amazonaws.services.s3.AmazonS3Client.<init>(Unknown Source)
    at com.addapps.taxiapp.utils.Utils.getS3Instance(AmazonS3Test.java)

I've little idea what that exception means, or why I cannot get an instance of AmazonS3.

I'm really struggling with S3, so if anyone can point me to any good resources I'd really appreciate it.


This exception was caused by my not having all the required AWS libraries in the project. AWS has a lot of libraries in different forms, and although not clear which are required, it won't work until the right ones are present.


Internally, the Amazon Web Service Client is using Apache commons HttpClient. From the stacktrace, it appears as if you have a library conflict. Check to see if you are using HttpClient somewhere else in your project. Most likely, you are using a different version than the Amazon Web Service Client is expecting.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜