我正在尝试在亚马逊S3服务中创建一个存储桶。但出现以下异常:
11-27 05:32:53.868: E/AndroidRuntime(1628): FATAL EXCEPTION: AsyncTask #1
11-27 05:32:53.868: E/AndroidRuntime(1628): java.lang.RuntimeException: An error occured while executing doInBackground()
11-27 05:32:53.868: E/AndroidRuntime(1628): at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-27 05:32:53.868: E/AndroidRuntime(1628): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.lang.Thread.run(Thread.java:856)
11-27 05:32:53.868: E/AndroidRuntime(1628): Caused by: com.amazonaws.services.s3.model.AmazonS3Exception: Status Code: 404, AWS Service: Amazon S3, AWS Request ID: A8BC718C70431BC1, AWS Error Code: NoSuchBucket, AWS Error Message: The specified bucket does not exist, S3 Extended Request ID: /LB0Sfqrke3aDRbuZ51iXbgdESeQFVVijrK8X8Csp2zDzppRQ9G/WOgy0fA71ltI
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.amazonaws.http.AmazonHttpClient.handleErrorResponse(AmazonHttpClient.java:666)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:330)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:182)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:3105)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1234)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.demo.amazons3cloudservice.MainActivity.create(MainActivity.java:104)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.demo.amazons3cloudservice.MainActivity$CreateBucket.doInBackground(MainActivity.java:71)
11-27 05:32:53.868: E/AndroidRuntime(1628): at com.demo.amazons3cloudservice.MainActivity$CreateBucket.doInBackground(MainActivity.java:1)
11-27 05:32:53.868: E/AndroidRuntime(1628): at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-27 05:32:53.868: E/AndroidRuntime(1628): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-27 05:32:53.868: E/AndroidRuntime(1628): ... 4 more
我从异常中了解到应用程序没有在aws s3上登录但是找不到方法,这是我的代码,请给我任何建议。
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.amazon.identity.auth.device.endpoint.RequestUrlBuilder;
import com.amazonaws.AmazonWebServiceRequest;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.http.AmazonHttpClient;
import com.amazonaws.http.HttpRequest;
import com.amazonaws.javax.xml.stream.xerces.impl.io.UTF8Reader;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class MainActivity extends Activity {
Button s3Connect;
private AmazonS3Client client;
String folderName = "new Folder";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
client = new AmazonS3Client(new BasicAWSCredentials(
Constants.ACCESS_KEY, Constants.ACCESS_SECRET));
s3Connect =(Button)findViewById(R.id.s3Connect);
s3Connect.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
CreateBucket bucket = new CreateBucket(getApplicationContext(),
client, folderName);
bucket.execute();
}
});
}
private class CreateBucket extends AsyncTask<Boolean, Void, Boolean> {
private Context mContext;
private AmazonS3Client mClient;
private String folderName;
public CreateBucket(Context mContext, AmazonS3Client mClient,
String folderName) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
this.mClient = mClient;
this.folderName = "new";
}
@Override
protected Boolean doInBackground(Boolean... arg0) {
// TODO Auto-generated method stub
create(folderName);
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (result == true)
Toast.makeText(getApplicationContext(),
"The folder is created", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(),
"The folder is not created", Toast.LENGTH_SHORT).show();
}
}
public void create(String foldername) {
// TODO validate foldername
// Create metadata for your folder & set content-length to 0
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(0);
// Create empty content
InputStream emptyContent = new ByteArrayInputStream(new byte[0]);
// Create a PutObjectRequest passing the foldername suffixed by /
PutObjectRequest putObjectRequest = new PutObjectRequest(
Constants.BUCKET, foldername + "/", emptyContent, metadata);
// Send request to S3 to create folder
client.putObject(putObjectRequest);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
确保您在代码中使用的密钥与存储桶中的密钥相同。请记住,Amazon S3密钥不区分大小写,这意味着您的密钥名称可能有一些不同的情况,如大写或小写。所以检查并再试一次......