Skip to content

Instantly share code, notes, and snippets.

@lpellegr
Last active June 25, 2021 00:38
Show Gist options
  • Save lpellegr/e2a05f24181c33a65467fbc189a2e115 to your computer and use it in GitHub Desktop.
Save lpellegr/e2a05f24181c33a65467fbc189a2e115 to your computer and use it in GitHub Desktop.
Scaleway Object Storage in Java using Amazon S3 SDK
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import java.io.InputStream;
import java.net.URI;
import java.util.function.Consumer;
public class ScalewayStorageService {
private final String SCALEWAY_ENDPOINT_URL = "https://s3.fr-par.scw.cloud";
private final String SCALEWAY_REGION = "fr-par";
private final StorageBucket bucket;
private final S3Client client;
public ScalewayStorageService(final StorageBucket bucket, final String accessKey, final String secretKey) {
this.bucket = bucket;
this.client =
S3Client.builder().region(Region.of(SCALEWAY_REGION))
.endpointOverride(URI.create(SCALEWAY_ENDPOINT_URL))
.credentialsProvider(
StaticCredentialsProvider.create(
AwsBasicCredentials.create(accessKey, secretKey)))
.build();
}
public void download(final String path, final Consumer<InputStream> handler) {
final GetObjectRequest objectRequest =
GetObjectRequest
.builder()
.key(path)
.bucket(bucket.getName())
.build();
handler.accept(client.getObject(objectRequest));
}
protected void shutdown() {
this.client.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment