· 7 years ago · Mar 22, 2018, 10:26 AM
1java.lang.IllegalArgumentException: TransferUtility has not been configured with a default bucket. Please use the corresponding method that specifies bucket name or configure the default bucket name in construction of the object. See TransferUtility.builder().defaultBucket() or TransferUtility.builder().awsConfiguration()
2
3///////////////////////////////////////UPLOAD TO S3 WITH KEY AND SECRETKEY///////////////////
4 // KEY and SECRET are gotten when we create an IAM user above
5 AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("city", cityName);
6 AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("type", entityType);
7 AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("entityid", mallId);
8 AWSCredentials.S3KEY = AWSCredentials.S3KEY.replace("fileName", Common.imageName);
9 Log.d("S3 KEY", AWSCredentials.S3KEY);
10
11 BasicAWSCredentials credentials = new BasicAWSCredentials(AWSCredentials.KEY, AWSCredentials.SECRET);
12 AmazonS3Client s3Client = new AmazonS3Client(credentials);
13 s3Client.putObject(new PutObjectRequest(AWSCredentials.BUCKET_NAME, AWSCredentials.KEY, new File(Common.imageLocation)));
14 //s3Client.setBucketAccelerateConfiguration(AWSCredentials.BUCKET_NAME);
15 //s3Client.setEndpoint("https://s3.ap-south-1.amazonaws.com/lipl-media/");
16 TransferUtility transferUtility = TransferUtility.builder()
17 .context(getApplicationContext())
18 .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
19 .s3Client(s3Client)
20 .build();
21
22/*try
23{
24 // FileInputStream fis = new FileInputStream(new
25 File(Common.imageName));
26}
27 catch(FileNotFoundException e)
28{
29e.printStackTrace();
30 }*/
31 TransferObserver uploadObserver =
32 transferUtility.upload(AWSCredentials.S3KEY, new File(Common.imageLocation));
33
34 uploadObserver.setTransferListener(new TransferListener() {
35
36 @Override
37 public void onStateChanged(int id, TransferState state) {
38 if (TransferState.COMPLETED == state) {
39 //Toast.makeText(FormBuilderActivity.this,"Upload Status:"+state.toString(),Toast.LENGTH_SHORT).show();
40 Log.d("S3 upload status", state.toString());
41 // Handle a completed download.
42 }
43 }
44
45 @Override
46 public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
47 float percentDonef = ((float)bytesCurrent/(float)bytesTotal) * 100;
48 int percentDone = (int)percentDonef;
49 }
50
51 @Override
52 public void onError(int id, Exception ex) {
53 //Toast.makeText(FormBuilderActivity.this,"Upload Error:"+ex.getLocalizedMessage(),Toast.LENGTH_SHORT).show();
54 Log.d("S3 upload fail", ex.getLocalizedMessage());
55 ex.printStackTrace();
56 // Handle errors
57 }
58
59 });
60 // If your upload does not trigger the onStateChanged method inside your
61 // TransferListener, you can directly check the transfer state as shown here.
62 if (TransferState.COMPLETED == uploadObserver.getState()) {
63 Log.d("S3 upload status", uploadObserver.getState().toString());
64 //Toast.makeText(FormBuilderActivity.this,"Upload Status:"+uploadObserver.getState().toString(),Toast.LENGTH_SHORT).show();
65 // Handle a completed upload.
66 }