AWS SDK - Java

Please read the following to understand how to use the AWS SDK - Java with the 4EVERLAND Bucket. Combine the AWS SDK API for the Java language with the 4EVERLAND Bucket to achieve file uploads to the IPFS or Arweave storage networks.

Preparation

Development Examples

Create a bucket

bucketName: The desired bucket name to create

accessKey: Bucket-Access Keys-API key

secretKey: Bucket-Access Keys-API Secret

endpoint_url: https://endpoint.4everland.co

region: Default filled in with "4everland"

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 = "new-bucketname";
        String accessKey = "4EVERLAND-Bucket-APIKey";
        String secretKey = "4EVERLAND-Bucket-APISecret";
        String endpoint_url = "https://endpoint.4everland.co";
        String region = "4everland";
        try {
            AmazonS3ClientBuilder.standard();
            AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(credentials)
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint_url, region))
                    .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 a file

bucketName: Your bucket name

stringObjKeyName: The name of the file to be uploaded

fileObjKeyName: The name of the file to be uploaded

fileName: The path of the file to be uploaded

accessKey: Bucket-Access Keys-API key

secretKey: Bucket-Access Keys-API Secret

endpoint_url: https://endpoint.4everland.co

region: Default filled in with "4everland"

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 = "4everland-bucketname";
        String stringObjKeyName = "object-name";
        String fileObjKeyName = "object-name";
        String fileName = "/path/to/4everland/4ever.png";
        String accessKey = "4EVERLAND-Bucket-APIKey";
        String secretKey = "4EVERLAND-Bucket-APISecret";
        String endpoint_url = "https://endpoint.4everland.co";
        String region = "4everland";

        try {
            AWSCredentials credentials = new BasicAWSCredentials(accessKey,secretKey);
            AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                    .withCredentials(credentials)
                    .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint_url, region))
                    .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();
        }
    }
}

Requesting IPFS CID and Arweave Hash

bucket: The bucket name where the target file is stored

key: The name of the target file

HeadObjectRequest headObjectRequest = HeadObjectRequest.builder()
  .bucket("4everland-bucketname")
  .key("your-object-name")
  .build();
  
HeadObjectResponse headObjectResponse = s3Client.headObject(headObjectRequest);
System.out.println("ipfs hash: " + headObjectResponse.metadata().get("ipfs-hash");
//If it is an Arweave type of bucket.
System.out.println("arweave hash: " + headObjectResponse.metadata().get("arweave-hash");

If you have any questions, please join our Discord server, or send us an email at contact@4everland.org.

Last updated