EOS provides an API for you to call in your code to upload and download objects.
Details
Identity
We recommend that you select a RAM user or RAM role to call API operations. For more information, see Identity.
Authorization
Before you call an operation, you need to obtain the required permissions. For more information, see Authorization management. The following policies grant the required permissions.
AliyunEnsFullAccess
This system policy is the read/write policy for Edge Node Service (ENS). This policy provides full access to ENS. Proceed with caution.
AliyunEnsEOSFullAccess
This system policy is the read/write policy for EOS. This policy provides full access to EOS.
Call methods
Most API operations of EOS are compatible with OSS. The unified endpoint of EOS is eos.aliyuncs.com, which is different from those of OSS. You can call EOS API operations from any region and over any network. Access over an internal network does not incur any Internet traffic fees.
EOS is not region bound, and therefore only few EOS operations are incompatible with OSS. For more information about how to call EOS API operations, see Integration overview.
The following table lists EOS API operations that are compatible with OSS.
You can call compatible API operations by using OSS SDKs. For more information, see Overview.
You can call incompatible API operations by using ENS SDKs. For more information, see Integration overview.
OpenAPI | Description | EOS OpenAPI | Compatible with OSS |
Bucket management | |||
ListBuckets | Lists all buckets that belong to the requester. | Yes | No |
PutBucket | Creates a bucket. | Yes | No |
DeleteBucket | Deletes a bucket. | Yes | Yes |
GetBucketInfo | Queries the information about a bucket. | Yes | Yes |
PutBucketLifecycle | Configures lifecycle rules for the objects in a bucket. | Yes | Yes |
GetBucketLifecycle | Queries the lifecycle rules configured for the objects in a bucket. | Yes | Yes |
DeleteBucketLifecycle | Deletes the lifecycle rules configured for the objects in a bucket. | Yes | Yes |
PutBucketAcl | Configures the access control list (ACL) of a bucket. | Yes | Yes |
GetBucketAcl | Queries the ACL of a bucket. | Yes | Yes |
Object management | |||
DeleteObject | Delete a single object. | Yes | Yes |
PutObject | Upload an object. | No | Yes |
GetObject | Downloads an object. | No | Yes |
AppendObject | Uploads an object by using append upload. | No | Yes |
DeleteMultipleObjects | Deletes multiple objects. | No | Yes |
CopyObject | Copies an object. Objects can be copied within a bucket. | No | Yes |
PutObjectTagging | Configures or updates the tags of an object. | No | Yes |
GetObjectTagging | Queries the tags of an object. | No | Yes |
DeleteObjectTagging | Deletes the tags of an object. | No | Yes |
HeadObject | Queries only the metadata of an object but not the object content. | No | Yes |
GetObjectMeta | Queries only the basic metadata of an object, including ETag, Size, and LastModified, but not the object content. | No | Yes |
Multipart upload | |||
InitiateMultipartUpload | Initiates a multipart upload task. | No | Yes |
UploadPart | Uploads an object in multiple parts. | No | Yes |
CompleteMultipartUpload | Completes the multipart upload task of an object. | No | Yes |
AbortMultipartUpload | Cancels a multipart upload task. | No | Yes |
Examples
Create a bucket
You can call EOS operations only in the Common mode.
package com.aliyun.sample;
import com.aliyun.tea.*;
public class Sample {
/**
* Use your AccessKey ID and AccessKey secret to initialize your client.
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.teaopenapi.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// Required. Specify your AccessKey ID.
.setAccessKeyId(accessKeyId)
// Required. Specify your AccessKey Secret.
.setAccessKeySecret(accessKeySecret);
// For more information about the endpoint, visit https://api.alibabacloud.com/product/Ens.
config.endpoint = "ens.aliyuncs.com";
return new com.aliyun.teaopenapi.Client(config);
}
/**
* API operation-related parameters
* @param path params
* @return OpenApi.Params
*/
public static com.aliyun.teaopenapi.models.Params createApiInfo() throws Exception {
com.aliyun.teaopenapi.models.Params params = new com.aliyun.teaopenapi.models.Params()
// The name of the API operation.
.setAction("PutBucket")
// The version of the API operation.
.setVersion("2017-11-10")
// The protocol of the API operation.
.setProtocol("HTTPS")
// The HTTP method of the operation.
.setMethod("POST")
.setAuthType("AK")
.setStyle("RPC")
// The path of the API operation.
.setPathname("/")
// The format of the request body.
.setReqBodyType("json")
// The format of the response body.
.setBodyType("json");
return params;
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
// Make sure that the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are configured.
// If the project code is leaked, the AccessKey pair may be disclosed, which may compromise the security of resources in your account. The following sample code is for reference only. We recommend that you use Security Token Service (STS)-based authentication. For more information about authentication methods, visit https://www.alibabacloud.com/help/sdk/developer-reference/credentials-1.
com.aliyun.teaopenapi.Client client = Sample.createClient(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
com.aliyun.teaopenapi.models.Params params = Sample.createApiInfo();
// body params
java.util.Map<String, Object> body = new java.util.HashMap<>();
// The name of the bucket.
body.put("BucketName", "global-eos-test1");
// The ACL of the bucket.
body.put("BucketAcl", "private");
// runtime options
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
com.aliyun.teaopenapi.models.OpenApiRequest request = new com.aliyun.teaopenapi.models.OpenApiRequest().setBody(body);
// After you copy and run the sample code, obtain the return value of the operation.
// The return value is of the Map type. The return value contains the following three types of data: response body, response header, and HTTP status code.
java.util.Map<String, ?> resp = client.callApi(params, request, runtime);
com.aliyun.teaconsole.Client.log(com.aliyun.teautil.Common.toJSONString(resp));
}
}
Upload an object
Object-related API operations of EOS are fully compatible with OSS and can be called by using OSS SDKs.
Sample code
package com.aliyun.sample;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;
import com.aliyuncs.ens.model.v20171110.*;
import java.io.ByteArrayInputStream;
public class Demo {
public static void main(String[] args) throws Exception {
String endpoint = "http://eos.aliyuncs.com";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Initialize the OSSClient instance.
OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
try {
// Specify the name of the bucket.
String bucketName = "global-eos-test1";
String objectName = "demoTestObject.txt";
String content = "this is demo"; // Create a PutObjectRequest object.
// Upload the string.
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
ossClient.putObject(putObjectRequest);
// Shut down the OSSClient instance.
ossClient.shutdown();
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason: ", e.);
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}For more information, see Simple upload.
Download an object
For information about how to download objects, see Overview. You can also download objects by using authorized URLs that are generated. For more information, see Authorize access.