快速入门

  1. 初始化一个BosClient。

    BosClient是与BOS服务交互的客户端,BOS Android SDK的BOS操作都是通过BosClient完成的。

    示例代码:

    BosClientConfiguration config = new BosClientConfiguration();
    config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>));   //您的AK/SK
    config.setEndpoint(<EndPoint>);    //传入Bucket所在区域域名
    BosClient client = new BosClient(config);
    
  2. 新建一个Bucket。

    Bucket是BOS上的命名空间,相当于数据的容器,可以存储若干数据实体(Object)。在您上传数据前,必须先创建一个Bucket。

    示例代码:

    BosClient client = new BosClient(config);    //创建BOSClient实例
    client.createBucket(<BucketName>);     //新建一个Bucket,指定Bucket名称
    
  3. 上传Object。

    Object是BOS中最基本的数据单元,您可以把Object简单的理解为文件。对于一个简单的Object的上传,BOS为您提供了四种方式:文件形式上传、数据流形式上传、二进制串上传和字符串上传。

    示例代码:

    // 获取指定文件
    File file = new File(<FilePath>);     //指定文件路径
    
    // 以文件形式上传Object
    PutObjectResponse putObjectFromFileResponse = client.putObject(<BucketName>, <ObjectKey>, file);
    
    // 获取数据流
    InputStream inputStream = new FileInputStream(<FilePath>);
    
    // 以数据流形式上传Object
    PutObjectResponse putObjectResponseFromInputStream = client.putObject(<BucketName>, <ObjectKey>, inputStream);
    
    // 以二进制串上传Object
    PutObjectResponse putObjectResponseFromByte = client.putObject(<BucketName>, <ObjectKey>, <byte>);
    
    // 以字符串上传Object
    PutObjectResponse putObjectResponseFromString = client.putObject(<BucketName>, <ObjectKey>, <string>);
    
    // 打印ETag
    System.out.println(putObjectFromFileResponse.getETag());
    
  4. 查看Bucket下的Object列表。

    当您完成一系列上传后,可以参考如下代码来查看Bucket下的全部Object。

    示例代码:

    // 获取指定Bucket下的所有Object信息
    ListObjectsResponse listing = client.listObjects(<BucketName>);
    
    // 遍历所有Object
    for (BosObjectSummary objectSummary : listing.getContents()) {
      System.out.println("ObjectKey: " + objectSummary.getKey());
      }
    
  5. 获取指定Object

    用户可以参考如下代码来实现对一个或者多个Object的获取。

    示例代码:

    // 获取Object,返回结果为BosObject对象
    BosObject object = client.getObject(<BucketName>, <ObjectKey>);
    
    // 获取ObjectMeta
    ObjectMetadata meta = object.getObjectMetadata();
    
    // 获取Object的输入流
    InputStream objectContent = object.getObjectContent();
    
    // 处理Object
    ...
    
    // 关闭流
    objectContent.close();
    

完整示例

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Bundle;

import com.baidubce.BceClientException;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.development.AppSettings;
import com.baidubce.development.BaseActivity;
import com.baidubce.development.R;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.BosObject;
import com.baidubce.services.bos.model.BosObjectSummary;
import com.baidubce.services.bos.model.CreateBucketResponse;
import com.baidubce.services.bos.model.ListObjectsResponse;
import com.baidubce.services.bos.model.ObjectMetadata;
import com.baidubce.services.bos.model.PutObjectResponse;
import com.baidubce.util.BLog;

public class ExampleActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //打开bossdk运行时log
        BLog.enableLog();

        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(<AccessKeyID>, <SecretAccessKey>)); 
        config.setEndpoint(<EndPoint>); //Bucket所在区域
        final BosClient client = new BosClient(config);

        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    //创建Bucket
                    CreateBucketResponse response = client.createBucket(<BucketName>); //新建一个Bucket并指定Bucket名称
                    System.out.println(response.getLocation());
                    System.out.println(response.getName());

                    //上传Object
                    File file = new File(<Path>);//上传文件的目录
                    PutObjectResponse putObjectFromFileResponse = client.putObject(<BucketName>, <ObjectKey>, file);
                    System.out.println(putObjectFromFileResponse.getETag());

                    //查看Object
                    ListObjectsResponse list = client.listObjects(<BucketName>);
                    for (BosObjectSummary objectSummary : list.getContents()) {
                        System.out.println("ObjectKey: " + objectSummary.getKey());
                    }

                    // 获取Object
                    BosObject object = client.getObject(<BucketName>, <ObjectKey>);
                    // 获取ObjectMeta
                    ObjectMetadata meta = object.getObjectMetadata();
                    // 获取Object的输入流
                    InputStream objectContent = object.getObjectContent();
                    // 处理Object
                    FileOutputStream fos=new FileOutputStream(<Path>);//下载文件的目录/文件名
                    byte[] buffer=new byte[2048];
                    int count=0;
                    while ((count=objectContent.read(buffer))>=0) {
                        fos.write(buffer,0,count);
                    }

                    // 关闭流
                    objectContent.close();
                    fos.close();
                    System.out.println(meta.getETag());
                    System.out.println(meta.getContentLength());

                }catch (BceServiceException e) {
                    System.out.println("Error ErrorCode: " + e.getErrorCode());
                    System.out.println("Error RequestId: " + e.getRequestId());
                    System.out.println("Error StatusCode: " + e.getStatusCode());
                    System.out.println("Error Message: " + e.getMessage());
                    System.out.println("Error ErrorType: " + e.getErrorType());
                } catch (BceClientException e) {
                    System.out.println("Error Message: " + e.getMessage());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }
        }).start();

    }
}