### Download an Object from TOS
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
Provides an example of downloading objects from a TOS bucket. It requires the bucket name, object key, and a local file path for saving the downloaded content. Error handling for TOS exceptions and IO operations is included.
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
String filePath = "your local file name to store downloaded file"; // eg. "/home/user/aaa.txt"
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucket).setKey(key);
try (GetObjectV2Output object = client.getObject(input);
FileOutputStream writer = new FileOutputStream(filePath)) {
if (object.getContent() != null) {
int once, total = 0;
byte[] buffer = new byte[4096];
InputStream inputStream = object.getContent();
while ((once = inputStream.read(buffer)) > 0) {
total += once;
writer.write(buffer, 0, once);
}
System.out.println("object's size is " + total + " bytes");
} else {
// return null if key not exist
System.out.println("key " + key + " not found");
}
} catch (TosException | IOException e) {
System.out.println("getObject error");
e.printStackTrace();
}
```
--------------------------------
### Install Volcengine TOS Java SDK via Maven
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
This snippet shows how to add the Volcengine TOS Java SDK as a dependency in your Maven project. Ensure you have Java 1.8 or later installed.
```xml
com.volcengine
ve-tos-java-sdk
2.8.8
```
--------------------------------
### Create a TOS Bucket
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
Demonstrates how to create a bucket in TOS using the SDK. A bucket serves as a unique namespace for storing data. Requires client initialization and a bucket name.
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
CreateBucketV2Input input = new CreateBucketV2Input().setBucket(bucket);
try{
CreateBucketV2Output output = client.createBucket(input);
System.out.println("Created bucket location is " + output.getLocation());
} catch (TosException e) {
System.out.println("Create bucket failed");
e.printStackTrace();
}
```
--------------------------------
### Initialize TOSV2Client
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
Initializes a TOSV2Client to interact with the TOS service. Requires region, endpoint, access key, and secret key as parameters.
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
```
--------------------------------
### Upload an Object to TOS
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
Shows how to upload a file as an object into a specified bucket. This involves creating a client, preparing input with bucket name, key, and content stream, and handling potential exceptions.
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
String data = "1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'";
InputStream stream = new ByteArrayInputStream(data.getBytes());
String key = "object-crud-"+System.currentTimeMillis();
try{
PutObjectBasicInput basicInput = new PutObjectBasicInput().setBucket(bucket).setKey(key);
PutObjectInput input = new PutObjectInput().setPutObjectBasicInput(basicInput).setContent(stream);
PutObjectOutput output = client.putObject(input);
System.out.println("Put object success, the object's etag is " + output.getEtag());
} catch (TosException e) {
System.out.println("Put object failed");
e.printStackTrace();
}
```
--------------------------------
### Maven 引入 ve-tos-java-sdk
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
展示了如何使用Maven将火山引擎TOS Java SDK添加到项目中。这是使用该SDK进行对象存储操作的前提。
```xml
com.volcengine
ve-tos-java-sdk
2.8.8
```
--------------------------------
### Gradle 引入 TOS Android SDK
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
在 Android 项目的 build.gradle 文件中添加火山引擎 TOS Android SDK 的依赖,以便在项目中使用 SDK 功能。此配置需要 Gradle 6.1.1 或更高版本。
```xml
dependencies {
implementation 'com.volcengine:ve-tos-android-sdk:2.8.8'
}
```
--------------------------------
### Android 项目 Java 8 支持配置
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
为 Android 项目配置 Java 8 支持,以兼容基于 Java 8 构建的 TOS Android SDK。这包括启用 coreLibraryDesugaring 和设置 source/target compatibility 为 Java 1.8。
```groovy
android {
compileOptions {
coreLibraryDesugaringEnabled true
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
}
```
--------------------------------
### 从 TOS 桶下载对象
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
从指定的 TOS 桶下载对象到本地文件。此示例展示了如何读取对象内容并将其写入本地文件,同时处理对象不存在或下载过程中可能发生的异常。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
String filePath = "your local file name to store downloaded file"; // eg. "/home/user/aaa.txt"
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucket).setKey(key);
try (GetObjectV2Output object = client.getObject(input);
FileOutputStream writer = new FileOutputStream(filePath)) {
if (object.getContent() != null) {
int once, total = 0;
byte[] buffer = new byte[4096];
InputStream inputStream = object.getContent();
while ((once = inputStream.read(buffer)) > 0) {
total += once;
writer.write(buffer, 0, once);
}
System.out.println("object's size is " + total + " bytes");
} else {
// key不存在返回null
System.out.println("key " + key + " not found");
}
} catch (TosException | IOException e) {
System.out.println("getObject error");
e.printStackTrace();
}
```
--------------------------------
### 创建 TOS 桶 (Java)
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
展示了如何使用TOS Java SDK创建一个新的桶。创建桶是存储对象的前提,桶是TOS中全局唯一的命名空间。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
CreateBucketV2Input input = new CreateBucketV2Input().setBucket(bucket);
try{
CreateBucketV2Output output = client.createBucket(input);
System.out.println("Created bucket location is " + output.getLocation());
} catch (TosException e) {
System.out.println("Create bucket failed");
e.printStackTrace();
}
```
--------------------------------
### 下载 TOS 对象到本地文件 (Java)
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
展示了如何从TOS桶中下载一个对象到指定的本地文件路径。代码处理了下载的数据流并写入文件,同时打印下载的文件大小或未找到对象的提示。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
String filePath = "your local file name to store downloaded file"; // eg. "/home/user/aaa.txt"
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
GetObjectV2Input input = new GetObjectV2Input().setBucket(bucket).setKey(key);
try (GetObjectV2Output object = client.getObject(input);
FileOutputStream writer = new FileOutputStream(filePath)) {
if (object.getContent() != null) {
int once, total = 0;
byte[] buffer = new byte[4096];
InputStream inputStream = object.getContent();
while ((once = inputStream.read(buffer)) > 0) {
total += once;
writer.write(buffer, 0, once);
}
System.out.println("object's size is " + total + " bytes");
} else {
// key不存在返回null
System.out.println("key " + key + " not found");
}
} catch (TosException | IOException e) {
System.out.println("getObject error");
e.printStackTrace();
}
```
--------------------------------
### 上传对象到 TOS 桶 (Java)
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
演示了如何将数据上传到已创建的TOS桶中。代码展示了如何准备数据流和使用PutObjectInput进行上传,并打印上传成功后的ETag。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
String data = "1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'";
InputStream stream = new ByteArrayInputStream(data.getBytes());
String key = "object-crud-"+System.currentTimeMillis();
try{
PutObjectBasicInput basicInput = new PutObjectBasicInput().setBucket(bucket).setKey(key);
PutObjectInput input = new PutObjectInput().setPutObjectBasicInput(basicInput).setContent(stream);
PutObjectOutput output = client.putObject(input);
System.out.println("Put object success, the object's etag is " + output.getEtag());
} catch (TosException e) {
System.out.println("Put object failed");
e.printStackTrace();
}
```
--------------------------------
### 创建 TOS 桶
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
创建一个新的 TOS 桶,用于存储对象。此操作需要提供桶名称,并返回桶的创建位置信息。如果创建失败,会抛出 TosException。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
CreateBucketV2Input input = new CreateBucketV2Input().setBucket(bucket);
try{
CreateBucketV2Output output = client.createBucket(input);
System.out.println("Created bucket location is " + output.getLocation());
} catch (TosException e) {
System.out.println("Create bucket failed");
e.printStackTrace();
}
```
--------------------------------
### 删除 TOS 桶中的对象
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
从指定的 TOS 桶中删除一个已存在的对象。此操作会返回删除操作的结果信息。如果删除失败,会抛出 TosException。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
DeleteObjectInput input = new DeleteObjectInput().setBucket(bucket).setKey(key);
try {
DeleteObjectOutput output = client.deleteObject(input);
System.out.println("Delete success, " + output);
} catch (TosException e) {
System.out.println("Delete failed");
e.printStackTrace();
}
```
--------------------------------
### 初始化 TOSV2 客户端 (Java)
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
演示了如何使用Access Key、Secret Key、Endpoint和Region初始化TOSV2Client实例。这是与TOS服务进行交互的第一步。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
```
--------------------------------
### 上传对象到 TOS 桶
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
将数据上传到指定的 TOS 桶中。此示例演示了如何将一个字符串数据作为对象上传,并返回上传对象的 etag 值。上传失败会抛出 TosException。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
String data = "1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'1234567890abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+<>?,./ :'";
InputStream stream = new ByteArrayInputStream(data.getBytes());
String key = "object-crud-"+System.currentTimeMillis();
try{
PutObjectBasicInput basicInput = new PutObjectBasicInput().setBucket(bucket).setKey(key);
PutObjectInput input = new PutObjectInput().setPutObjectBasicInput(basicInput).setContent(stream);
PutObjectOutput output = client.putObject(input);
System.out.println("Put object success, the object's etag is " + output.getEtag());
} catch (TosException e) {
System.out.println("Put object failed");
e.printStackTrace();
}
```
--------------------------------
### Delete an Object from TOS
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README.md
Demonstrates how to delete objects from a TOS bucket using the SDK. This operation requires the bucket name and the key of the object to be deleted, along with proper error handling.
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
DeleteObjectInput input = new DeleteObjectInput().setBucket(bucket).setKey(key);
try {
DeleteObjectOutput output = client.deleteObject(input);
System.out.println("Delete success, " + output);
} catch (TosException e) {
System.out.println("Delete failed");
e.printStackTrace();
}
```
--------------------------------
### 初始化 TOSV2 客户端
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-Android.md
初始化 TOSV2Client 实例以连接到火山引擎 TOS 服务。初始化需要提供区域(region)、访问节点(endpoint)、访问密钥(accessKey)和秘密密钥(secretKey)。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
```
--------------------------------
### 删除 TOS 桶中的对象 (Java)
Source: https://github.com/volcengine/ve-tos-java-sdk/blob/main/README-zh.md
演示了如何使用TOS Java SDK从指定的桶中删除一个对象。代码展示了如何构建DeleteObjectInput并调用deleteObject方法,同时处理可能的异常。
```java
String region = "region to access";
String endpoint = "endpoint to access";
String accessKey = "your access key";
String secretKey = "your secret key";
String bucket = "your bucket name";
String key = "your key name";
TOSV2 client = new TOSV2ClientBuilder().build(region, endpoint, accessKey, secretKey);
DeleteObjectInput input = new DeleteObjectInput().setBucket(bucket).setKey(key);
try {
DeleteObjectOutput output = client.deleteObject(input);
System.out.println("Delete success, " + output);
} catch (TosException e) {
System.out.println("Delete failed");
e.printStackTrace();
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.