Code Development + SDKs Code Development AWS SDK - Java Learn how to use the AWS SDK for Java with Filebase.
What is AWS SDK - Java?
AWS SDKs (software development kits) help simplify coding and application development by supporting and providing code objects for use with S3-compatible services. There are a variety of different AWS SDKs, each for a different coding language. This guide covers AWS SDK - Java.
This guide was written using Ubuntu 20.04 and tested using the command line interface. If you are using an Integrated Development Environment (IDE), the installation of dependencies and modules will vary.
Create a Bucket
The following code example creates a new Filebase bucket. Replace the following values in the code to match your configuration:
new-filebase-bucket: The Intended New Bucket Name
Bucket names must be unique across all Filebase users, be between 3 and 63 characters long, and can contain only lowercase characters, numbers, and dashes.
Buckets created through this method will be automatically created on the IPFS network.
Copy import com . amazonaws . AmazonServiceException ;
import com . amazonaws . SdkClientException ;
import com . amazonaws . auth . profile . ProfileCredentialsProvider ;
import com . amazonaws . regions . Regions ;
import com . amazonaws . services . s3 . AmazonS3 ;
import com . amazonaws . services . s3 . AmazonS3ClientBuilder ;
import com . amazonaws . services . s3 . model . CreateBucketRequest ;
import com . amazonaws . services . s3 . model . GetBucketLocationRequest ;
import java . io . IOException ;
public class CreateBucket2 {
public static void main ( String [] args) throws IOException {
String bucketName = "filebase-bucket-name" ;
try {
AmazonS3ClientBuilder . standard ();
AmazonS3 s3Client = AmazonS3ClientBuilder . standard ()
. withCredentials ( new ProfileCredentialsProvider() )
. withEndpointConfiguration ( new AwsClientBuilder . EndpointConfiguration ( "s3.filebase.com" , "us-east-1" ))
. build ();
if ( ! s3Client . doesBucketExistV2 (bucketName)) {
s3Client . createBucket ( new CreateBucketRequest(bucketName) );
String bucketLocation = s3Client . getBucketLocation ( new GetBucketLocationRequest(bucketName) );
System . out . println ( "Bucket location: " + bucketLocation);
}
} catch ( AmazonServiceException e) {
e . printStackTrace ();
} catch ( SdkClientException e) {
e . printStackTrace ();
}
}
}
Upload an Object
The following code example uploads an object to the specified bucket. Replace the following values in the code to match your configuration:
filebase-bucket-name: The Filebase Bucket Name
/path/to/object/to/upload : The file path to the object that you want to be uploaded
object-name : The desired object name once uploaded
Copy import com . amazonaws . AmazonServiceException ;
import com . amazonaws . SdkClientException ;
import com . amazonaws . regions . Regions ;
import com . amazonaws . services . s3 . AmazonS3 ;
import com . amazonaws . services . s3 . AmazonS3ClientBuilder ;
import com . amazonaws . services . s3 . model . ObjectMetadata ;
import com . amazonaws . services . s3 . model . PutObjectRequest ;
import java . io . File ;
import java . io . IOException ;
public class UploadObject {
public static void main ( String [] args) throws IOException {
String bucketName = "filebase-bucket-name" ;
String stringObjKeyName = "object-name" ;
String fileObjKeyName = "object-name" ;
String fileName = "/path/to/object/to/upload" ;
try {
AmazonS3 s3Client = AmazonS3ClientBuilder . standard ()
. withEndpointConfiguration ( new AwsClientBuilder . EndpointConfiguration ( "s3.filebase.com" , "us-east-1" ))
. build ();
s3Client . putObject (bucketName , stringObjKeyName , "Uploaded String Object" );
PutObjectRequest request = new PutObjectRequest(bucketName , fileObjKeyName , new File(fileName)) ;
ObjectMetadata metadata = new ObjectMetadata() ;
metadata . setContentType ( "plain/text" );
metadata . addUserMetadata ( "title" , "someTitle" );
request . setMetadata (metadata);
s3Client . putObject (request);
} catch ( AmazonServiceException e) {
e . printStackTrace ();
} catch ( SdkClientException e) {
e . printStackTrace ();
}
}
}
Download an Object
The following code example downloads an object from the specified bucket. Replace the following values in the code to match your configuration:
filebase-bucket-name: The Filebase Bucket Name
object-name : The object to be downloaded
Copy import com . amazonaws . AmazonServiceException ;
import com . amazonaws . SdkClientException ;
import com . amazonaws . auth . profile . ProfileCredentialsProvider ;
import com . amazonaws . regions . Regions ;
import com . amazonaws . services . s3 . AmazonS3 ;
import com . amazonaws . services . s3 . AmazonS3ClientBuilder ;
import com . amazonaws . services . s3 . model . GetObjectRequest ;
import com . amazonaws . services . s3 . model . ResponseHeaderOverrides ;
import com . amazonaws . services . s3 . model . S3Object ;
import java . io . BufferedReader ;
import java . io . IOException ;
import java . io . InputStream ;
import java . io . InputStreamReader ;
public class GetObject2 {
public static void main ( String [] args) throws IOException {
String bucketName = "filebase-bucket-name" ;
String key = "object-name" ;
S3Object fullObject = null , objectPortion = null , headerOverrideObject = null ;
try {
AmazonS3 s3Client = AmazonS3ClientBuilder . standard ()
. withEndpointConfiguration ( new AwsClientBuilder . EndpointConfiguration ( "s3.filebase.com" , "us-east-1" ))
. withCredentials ( new ProfileCredentialsProvider() )
. build ();
System . out . println ( "Downloading an object" );
fullObject = s3Client . getObject ( new GetObjectRequest(bucketName , key) );
System . out . println ( "Content-Type: " + fullObject . getObjectMetadata () . getContentType ());
System . out . println ( "Content: " );
displayTextInputStream( fullObject . getObjectContent()) ;
GetObjectRequest rangeObjectRequest = new GetObjectRequest(bucketName , key)
. withRange ( 0 , 9 );
objectPortion = s3Client . getObject (rangeObjectRequest);
System . out . println ( "Printing bytes retrieved." );
displayTextInputStream( objectPortion . getObjectContent()) ;
ResponseHeaderOverrides headerOverrides = new ResponseHeaderOverrides()
. withCacheControl ( "No-cache" )
. withContentDisposition ( "attachment; filename=example.txt" );
GetObjectRequest getObjectRequestHeaderOverride = new GetObjectRequest(bucketName , key)
. withResponseHeaders (headerOverrides);
headerOverrideObject = s3Client . getObject (getObjectRequestHeaderOverride);
displayTextInputStream( headerOverrideObject . getObjectContent()) ;
} catch ( AmazonServiceException e) {
e . printStackTrace ();
} catch ( SdkClientException e) {
e . printStackTrace ();
} finally {
if (fullObject != null ) {
fullObject . close ();
}
if (objectPortion != null ) {
objectPortion . close ();
}
if (headerOverrideObject != null ) {
headerOverrideObject . close ();
}
}
}
private static void displayTextInputStream ( InputStream input) throws IOException {
BufferedReader reader = new BufferedReader( new InputStreamReader(input)) ;
String line = null ;
while ((line = reader . readLine ()) != null ) {
System . out . println (line);
}
System . out . println ();
}
}
Delete an Object
The following code example deletes an object from the specified bucket. Replace the following values in the code to match your configuration:
filebase-bucket-name: The Filebase Bucket Name
object-name : The object to be downloaded
Copy import com . amazonaws . AmazonServiceException ;
import com . amazonaws . SdkClientException ;
import com . amazonaws . auth . profile . ProfileCredentialsProvider ;
import com . amazonaws . regions . Regions ;
import com . amazonaws . services . s3 . AmazonS3 ;
import com . amazonaws . services . s3 . AmazonS3ClientBuilder ;
import com . amazonaws . services . s3 . model . DeleteObjectRequest ;
import java . io . IOException ;
public class DeleteObjectNonVersionedBucket {
public static void main ( String [] args) throws IOException {
String bucketName = "filebase-bucket-name" ;
String keyName = "object-name" ;
try {
AmazonS3 s3Client = AmazonS3ClientBuilder . standard ()
. withEndpointConfiguration ( new AwsClientBuilder . EndpointConfiguration ( "s3.filebase.com" , "us-east-1" ))
. withCredentials ( new ProfileCredentialsProvider() )
. build ();
s3Client . deleteObject ( new DeleteObjectRequest(bucketName , keyName) );
} catch ( AmazonServiceException e) {
e . printStackTrace ();
} catch ( SdkClientException e) {
e . printStackTrace ();
}
}
}
Last updated 4 months ago