Вам нужно всего несколько клиентских классов и ни один из классов учетных данных. Вот пример, который может помочь вам начать:
import java.sql.Timestamp;
import java.io.File;
import com.ibm.cloud.objectstorage.ClientConfiguration;
import com.ibm.cloud.objectstorage.SDKGlobalConfiguration;
import com.ibm.cloud.objectstorage.client.builder.AwsClientBuilder.EndpointConfiguration;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3;
import com.ibm.cloud.objectstorage.services.s3.AmazonS3ClientBuilder;
import com.ibm.cloud.objectstorage.services.s3.model.ListObjectsRequest;
import com.ibm.cloud.objectstorage.services.s3.model.ObjectListing;
import com.ibm.cloud.objectstorage.services.s3.model.S3ObjectSummary;
public class CredentialsFile
{
private static AmazonS3 _s3Client;
/**
* @param args
*/
public static void main(String[] args)
{
SDKGlobalConfiguration.IAM_ENDPOINT = "https://iam.bluemix.net/oidc/token";
String bucketName = "<bucket-name.";
String objectKey = "<object-key";
String filePath = "/absolute/path/to/file";
String endpoint_url = "https://s3-api.us-geo.objectstorage.softlayer.net";
String location = "us";
System.out.println("Current time: " + new Timestamp(System.currentTimeMillis()).toString());
_s3Client = createClient(endpoint_url, location);
newObject(bucketName, objectKey, filePath, _s3Client);
listObjects(bucketName, _s3Client);
}
/**
* @param bucketName
* @param clientNum
* @param endpoint_url
* @param location
* @return AmazonS3
*/
public static AmazonS3 createClient(String endpoint_url, String location)
{
ClientConfiguration clientConfig = new ClientConfiguration().withRequestTimeout(5000);
clientConfig.setUseTcpKeepAlive(true);
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new EndpointConfiguration(endpoint_url, location)).withPathStyleAccessEnabled(true)
.withClientConfiguration(clientConfig).build();
return s3Client;
}
/**
* @param bucketName
* @param keyName
* @param filePath
* @param s3Client
*/
public static void newObject(String bucketName, String keyName, String filePath, AmazonS3 s3Client)
{
System.out.println("Uploading new object " + keyName + " from " + filePath + "...");
s3Client.putObject(bucketName, keyName, new File(filePath));
System.out.println(keyName +" uploaded successfully.");
}
/**
* @param bucketName
* @param s3Client
*/
public static void listObjects(String bucketName, AmazonS3 s3Client)
{
System.out.println("Listing objects in bucket " + bucketName);
ObjectListing objectListing = s3Client.listObjects(new ListObjectsRequest().withBucketName(bucketName));
for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")");
}
System.out.println();
}
}