baidu云存储java sdk源码阅读之请求发送

      这个系列的文章中断了很长时间,今天继续写。我们首先来看BaiduBCS的入口类

public class BaiduBCS
{
  private static final Log log = LogFactory.getLog(BaiduBCS.class);
  private BCSHttpClient bcsHttpClient = null;
  private BCSCredentials credentials = null;
  private String endpoint = null;

  public BaiduBCS(BCSCredentials paramBCSCredentials, String paramString)
  {
    this.credentials = paramBCSCredentials;
    setEndpoint(paramString);
    this.bcsHttpClient = new BCSHttpClient(new ClientConfiguration());
  }

  public BaiduBCS(BCSCredentials paramBCSCredentials, String paramString, ClientConfiguration paramClientConfiguration)
  {
    this.credentials = paramBCSCredentials;
    setEndpoint(paramString);
    this.bcsHttpClient = new BCSHttpClient(paramClientConfiguration);
  }

   从构造函数来看bcsCredential是必须得,后一个就是endpoint,其实就是host,最后是客户端配置,默认是ClientConfiguration,从这个里面可以看到可以传递一个ClientConfirguration对象。

    我们接下来再看一下一个具体的动作,以copy文件为例:

  public BaiduBCSResponse<Empty> copyObject(CopyObjectRequest paramCopyObjectRequest)
    throws BCSClientException, BCSServiceException
  {
    assertParameterNotNull(paramCopyObjectRequest, "The request parameter can be null.");
    assertParameterNotNull(paramCopyObjectRequest.getHttpMethod(), "The http method parameter in Request must be specified.");
    assertParameterNotNull(paramCopyObjectRequest.getSource().getBucket(), "The bucket parameter of source must be specified when copy an object.");

    assertParameterNotNull(paramCopyObjectRequest.getSource().getObject(), "The object parameter of source must be specified when copy an object.");

    assertParameterNotNull(paramCopyObjectRequest.getDest().getBucket(), "The bucket parameter of dest must be specified when copy an object.");
    assertParameterNotNull(paramCopyObjectRequest.getDest().getObject(), "The object parameter of dest must be specified when copy an object.");
    log.debug("copy object, src[Bucket:" + paramCopyObjectRequest.getSource().getBucket() + "][Object:" + paramCopyObjectRequest.getSource().getObject() + "] to dest[Bucket" + paramCopyObjectRequest.getDest().getBucket() + "][Object" + paramCopyObjectRequest.getDest().getObject() + "]");

    BCSHttpRequest localBCSHttpRequest = createHttpRequest(paramCopyObjectRequest);

    localBCSHttpRequest.addHeader("x-bs-copy-source", "bs://" + paramCopyObjectRequest.getSource().getBucket() + paramCopyObjectRequest.getSource().getObject());

    if (null != paramCopyObjectRequest.getSourceEtag()) {
      localBCSHttpRequest.addHeader("x-bs-copy-source-tag", paramCopyObjectRequest.getSourceEtag());
    }

    if (null != paramCopyObjectRequest.getSourceDirective()) {
      localBCSHttpRequest.addHeader("x-bs-copy-source-directive", paramCopyObjectRequest.getSourceDirective());
    }

    populateRequestMetadata(localBCSHttpRequest, paramCopyObjectRequest.getDestMetadata());

    return this.bcsHttpClient.execute(localBCSHttpRequest, new VoidResponseHandler());
  }

     从代码的整体结构来看,是通过一个request对象,创建一个httpRequest对象,然后通过bcsHttpClient的execute方法执行,一个参数是HttpRequest,一个是ResponseHandler,从这个地方来看,只需要构造响应的request和响应的responseHandler就好。

你可能感兴趣的:(baidu bac,云存储)