Skip to content

Commit decc654

Browse files
author
Jevgeni Koltšin
committed
removed hardcoded s3 keys-> migrated to GetAmazonTokenRequest
1 parent 3f11339 commit decc654

3 files changed

Lines changed: 28 additions & 14 deletions

File tree

src/main/java/com/creatubbles/api/CreatubblesAPI.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package com.creatubbles.api;
22

33
import com.creatubbles.api.core.Creation;
4+
import com.creatubbles.api.core.Credentials;
45
import com.creatubbles.api.core.Gallery;
5-
import com.creatubbles.api.core.Image;
6+
import com.creatubbles.api.request.amazon.GetAmazonTokenRequest;
67
import com.creatubbles.api.request.amazon.UploadS3ImageRequest;
78
import com.creatubbles.api.request.auth.SignInRequest;
89
import com.creatubbles.api.request.creation.UpdateCreationRequest;
910
import com.creatubbles.api.request.creation.UploadCreationRequest;
11+
import com.creatubbles.api.response.amazon.GetAmazonTokenResponse;
1012
import com.creatubbles.api.response.auth.SignInResponse;
1113
import com.creatubbles.api.response.auth.SignUpResponse;
12-
import com.creatubbles.api.response.creation.UpdateCreationResponse;
1314
import com.creatubbles.api.response.creation.UploadCreationResponse;
1415
import com.creatubbles.api.response.creator.CreateCreatorResponse;
1516
import com.creatubbles.api.response.gallery.CreateUserGalleryResponse;
@@ -45,18 +46,26 @@ public static String buildURL(final String endPoint) {
4546
}
4647

4748
public static void main(String[] args) throws IOException {
49+
//login
4850
SignInResponse response = new SignInRequest("jevgeni.koltsin@gmail.com", "ccttbb").execute().getResponse();
4951
System.out.println(response.access_token);
52+
//create creation
5053
UploadCreationResponse uploadResponse = new UploadCreationRequest(response.access_token).execute().getResponse();
54+
//get required info for s3
55+
GetAmazonTokenResponse amazonTokenResponse = new GetAmazonTokenRequest(response.access_token).execute().getResponse();
56+
Credentials credentials = amazonTokenResponse.credentials;
5157
//TODO: last screenshot
5258
File file = new File("C:/dev/1.png");
5359
byte[] data = Files.readAllBytes(file.toPath());
5460
String fileName = System.currentTimeMillis() + "creation.png";
5561
Creation creation = uploadResponse.creation;
5662
String relativePath = creation.store_dir + "/" + fileName;
57-
new UploadS3ImageRequest(data, relativePath).execute().getResponse();
63+
//upload image
64+
new UploadS3ImageRequest(data, relativePath, credentials.access_key_id, credentials.secret_access_key, credentials.session_token)
65+
.execute().getResponse();
5866
creation.url = relativePath;
59-
UpdateCreationResponse updateResponse = new UpdateCreationRequest(response.access_token, creation).execute().getResponse();
67+
//update creation(url)
68+
new UpdateCreationRequest(response.access_token, creation).execute().getResponse();
6069
System.exit(0);
6170
}
6271
}

src/main/java/com/creatubbles/api/request/amazon/UploadS3ImageRequest.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.amazonaws.services.s3.model.CannedAccessControlList;
55
import com.amazonaws.services.s3.model.ObjectMetadata;
66
import com.amazonaws.services.s3.model.PutObjectRequest;
7-
import com.amazonaws.services.s3.model.PutObjectResult;
87
import com.creatubbles.api.core.CreatubblesRequest;
98
import com.creatubbles.api.response.amazon.UploadS3ImageResponse;
109
import com.creatubbles.api.util.S3ClientUtil;
@@ -18,11 +17,17 @@ public class UploadS3ImageRequest extends CreatubblesRequest<UploadS3ImageRespon
1817

1918
private String filePath;
2019
private byte[] data;
20+
private String accessKey;
21+
private String secretKey;
22+
private String sessionToken;
2123

22-
public UploadS3ImageRequest(byte[] data, String filePath) {
24+
public UploadS3ImageRequest(byte[] data, String filePath, String accessKey, String secretKey, String sessionToken) {
2325
super(null, null);
2426
this.filePath = filePath;
2527
this.data = data;
28+
this.accessKey = accessKey;
29+
this.secretKey = secretKey;
30+
this.sessionToken = sessionToken;
2631
}
2732

2833
@Override
@@ -33,7 +38,7 @@ public Class<? extends UploadS3ImageResponse> getResponseClass() {
3338
@Override
3439
public CreatubblesRequest<UploadS3ImageResponse> execute() {
3540
resetResponse();
36-
AmazonS3 client = S3ClientUtil.getClient();
41+
AmazonS3 client = S3ClientUtil.getClient(accessKey, secretKey, sessionToken);
3742
ObjectMetadata metadata = new ObjectMetadata();
3843
metadata.setContentLength(data.length);
3944
PutObjectRequest putObjectRequest = new PutObjectRequest(S3ClientUtil.AWS_S3_BUCKET_NAME, filePath, new ByteArrayInputStream(data), metadata)
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
package com.creatubbles.api.util;
22

33
import com.amazonaws.auth.AWSCredentials;
4-
import com.amazonaws.auth.BasicAWSCredentials;
4+
import com.amazonaws.auth.BasicSessionCredentials;
55
import com.amazonaws.services.s3.AmazonS3;
66
import com.amazonaws.services.s3.AmazonS3Client;
77

88
/**
99
* Created by Jevgeni on 27.10.2015.
1010
*/
1111
public class S3ClientUtil {
12-
private static final String AWS_S3_ACCESS_KEY = "AKIAJJCG5PC6P7E7URLQ";
13-
private static final String AWS_S3_SECRET_KEY = "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";
14-
private static final AWSCredentials AWS_S3_CREDENTIALS = new BasicAWSCredentials(AWS_S3_ACCESS_KEY, AWS_S3_SECRET_KEY);
15-
private static final AmazonS3 s3Client = new AmazonS3Client(AWS_S3_CREDENTIALS);
1612
public static final String AWS_S3_BUCKET_NAME = "ctbstaging";
1713
public static final String AWS_S3_BASE_URL = "http://" + AWS_S3_BUCKET_NAME + ".s3.amazonaws.com/";
1814

19-
public static AmazonS3 getClient() {
20-
return s3Client;
15+
public static AmazonS3 getClient(String accessKey, String secretKey, String sessionToken) {
16+
return new AmazonS3Client(getCredentials(accessKey, secretKey, sessionToken));
17+
}
18+
19+
private static AWSCredentials getCredentials(String accessKey, String secretKey, String sessionToken) {
20+
return new BasicSessionCredentials(accessKey, secretKey, sessionToken);
2121
}
2222
}

0 commit comments

Comments
 (0)