[Check If a Specified Key Exists in a Given S3 Bucket using Java] added code examples (#13775)
* [Check If a Specified Key Exists in a Given S3 Bucket using Java]] added code examples for change test name remove link from README.md * - used env variables for AWS credentials - used codestyle profile of baledung
This commit is contained in:
parent
bb9e5b65b4
commit
97bff9fd50
|
@ -0,0 +1,42 @@
|
||||||
|
package com.baeldung.s3;
|
||||||
|
|
||||||
|
import org.apache.http.HttpStatus;
|
||||||
|
|
||||||
|
import com.amazonaws.AmazonServiceException;
|
||||||
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
|
|
||||||
|
public class AWSS3ObjectUtils {
|
||||||
|
|
||||||
|
private AmazonS3 s3Client;
|
||||||
|
|
||||||
|
public AWSS3ObjectUtils(AmazonS3 s3client) {
|
||||||
|
this.s3Client = s3client;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doesObjectExistByDefaultMethod(String bucket, String key) {
|
||||||
|
return s3Client.doesObjectExist(bucket, key);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doesObjectExistByListObjects(String bucket, String key) {
|
||||||
|
return s3Client.listObjects(bucket)
|
||||||
|
.getObjectSummaries()
|
||||||
|
.stream()
|
||||||
|
.filter(s3ObjectSummary -> s3ObjectSummary.getKey()
|
||||||
|
.equals(key))
|
||||||
|
.findFirst()
|
||||||
|
.isPresent();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean doesObjectExistByMetaData(String bucket, String key) {
|
||||||
|
try {
|
||||||
|
s3Client.getObjectMetadata(bucket, key);
|
||||||
|
return true;
|
||||||
|
} catch (AmazonServiceException e) {
|
||||||
|
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
package com.baeldung.s3;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
|
||||||
|
import com.amazonaws.regions.Regions;
|
||||||
|
import com.amazonaws.services.s3.AmazonS3;
|
||||||
|
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
|
||||||
|
|
||||||
|
public class AWSS3ObjectIntegrationTest {
|
||||||
|
|
||||||
|
private static final String BUCKET = "your-bucket";
|
||||||
|
private static final String KEY_THAT_EXIST = "your-key-that-exist";
|
||||||
|
private AWSS3ObjectUtils s3ObjectUtils;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
AmazonS3 client = AmazonS3ClientBuilder.standard()
|
||||||
|
.withRegion(Regions.DEFAULT_REGION)
|
||||||
|
.withCredentials(new EnvironmentVariableCredentialsProvider())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
s3ObjectUtils = new AWSS3ObjectUtils(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenVerifyIfObjectExistByDefaultMethod_thenCorrect() {
|
||||||
|
assertTrue(s3ObjectUtils.doesObjectExistByDefaultMethod(BUCKET, KEY_THAT_EXIST), "Key: " + KEY_THAT_EXIST + " doesn't exist");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenVerifyIfObjectExistByListObjects_thenCorrect() {
|
||||||
|
assertTrue(s3ObjectUtils.doesObjectExistByListObjects(BUCKET, KEY_THAT_EXIST), "Key: " + KEY_THAT_EXIST + " doesn't exist");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenVerifyIfObjectExistByMetaData_thenCorrect() {
|
||||||
|
assertTrue(s3ObjectUtils.doesObjectExistByMetaData(BUCKET, KEY_THAT_EXIST), "Key: " + KEY_THAT_EXIST + " doesn't exist");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue