# 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

* In your development environment, [download and install](https://www.java.com/download/ie_manual.jsp) Java.
* [Download and install](https://github.com/aws/aws-sdk-go#getting-started) the AWS SDK for Java.
* [Register](https://www.4everland.org/) a free 4EVERLAND account.
* If you need to use Arweave storage, you need to create an Arweave bucket. Click to[ ](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible)[learn more](https://docs.4everland.org/storage/bucket/guides).
* To obtain the corresponding API key in the bucket, click to[ learn more](https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible).

## 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"

```java
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"

```java
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

```java
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");
```

{% hint style="info" %}
If you have any questions, please join our [Discord server](https://discord.com/invite/Cun2VpsdjF), or send us an email at <contact@4everland.org>.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.4everland.org/storage/bucket/bucket-api-s3-compatible/coding-examples/aws-sdk-java.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
