通过SDK下载文件

下面是Java样例代码

import java.io.IOException;

import com.xiaomi.infra.galaxy.fds.client.FDSClientConfiguration;
import com.xiaomi.infra.galaxy.fds.client.GalaxyFDS;
import com.xiaomi.infra.galaxy.fds.client.GalaxyFDSClient;
import com.xiaomi.infra.galaxy.fds.client.credential.BasicFDSCredential;
import com.xiaomi.infra.galaxy.fds.client.credential.GalaxyFDSCredential;
import com.xiaomi.infra.galaxy.fds.client.exception.GalaxyFDSClientException;
import com.xiaomi.infra.galaxy.fds.client.model.FDSObject;
import com.xiaomi.infra.galaxy.fds.client.model.FDSObjectInputStream;

public class FDSDemo {

  private static final String APP_ACCESS_KEY = ""; // 云服务密钥AppKey
  private static final String APP_ACCESS_SECRET = ""; // 云服务密钥AppSecret

  private static final String BUCKET_NAME = "fds-demo"; // 创建的Bucket名字
  private static final String OBJECT_NAME = "10MB.bin"; // 上传的Object名字

  public static void main(String[] args)
      throws GalaxyFDSClientException, IOException {
    GalaxyFDSCredential credential = new BasicFDSCredential(
        APP_ACCESS_KEY, APP_ACCESS_SECRET);

    // 构造Client
    String endpoint = "awsbj0.fds.api.xiaomi.com"; // 确认与Bucket所在region一致
    FDSClientConfiguration fdsConfig = new FDSClientConfiguration(endpoint);
    fdsConfig.enableHttps(true);
    fdsConfig.enableCdnForDownload(false);
    GalaxyFDS fdsClient = new GalaxyFDSClient(credential, fdsConfig);

    // 下载文件前1k内容到buffer里
    FDSObject object = fdsClient.getObject(BUCKET_NAME, OBJECT_NAME);
    FDSObjectInputStream in = object.getObjectContent();
    byte[] buffer = new byte[1024];
    int totalReadLen = 0;
    int readLen = 0;
    while ((readLen = in.read(buffer, totalReadLen,
        buffer.length - totalReadLen)) > 0) {
      totalReadLen += readLen;
    }
    in.close();
  }
}

添加pom依赖:

<dependencies>
  <dependency>
    <groupId>com.xiaomi.infra.galaxy</groupId>
    <artifactId>galaxy-fds-sdk-java</artifactId>
    <version>3.0.13</version>
  </dependency>
</dependencies>

results matching ""

    No results matching ""