content: /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing,
   * software distributed under the License is distributed on an
   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   * KIND, either express or implied.  See the License for the
   * specific language governing permissions and limitations
   * under the License.
   */
  
  package samples;
  
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.List;
  
  import com.aliyun.oss.ClientException;
  import com.aliyun.oss.OSS;
  import com.aliyun.oss.OSSClientBuilder;
  import com.aliyun.oss.OSSException;
  import com.aliyun.oss.model.CompleteMultipartUploadRequest;
  import com.aliyun.oss.model.GenericRequest;
  import com.aliyun.oss.model.GetObjectRequest;
  import com.aliyun.oss.model.InitiateMultipartUploadRequest;
  import com.aliyun.oss.model.InitiateMultipartUploadResult;
  import com.aliyun.oss.model.ListObjectsRequest;
  import com.aliyun.oss.model.OSSObject;
  import com.aliyun.oss.model.OSSObjectSummary;
  import com.aliyun.oss.model.ObjectListing;
  import com.aliyun.oss.model.PartETag;
  import com.aliyun.oss.model.Payer;
  import com.aliyun.oss.model.PutObjectRequest;
  import com.aliyun.oss.model.UploadPartRequest;
  import com.aliyun.oss.model.UploadPartResult;
  
  /**
   * Example for operate object by a third party.
   * You must authorise to a third party before run this sample.
   */
  public class ObjectRequestPaymentSample {
  
      private static String endpoint = "*** Provide OSS endpoint ***";
      private static String accessKeyId = "*** Provide your AccessKeyId ***";
      private static String accessKeySecret = "*** Provide your AccessKeySecret ***";
  
      private static String bucketName = "*** Provide bucket name ***";
      private static String objectName = "Provide object name ***";
      private static Payer payer = Payer.Requester;
  
      public static void main(String[] args) throws Throwable {
          // Create oss client
          OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
          try {
  
              // list objects by specifying request payer
              ListObjectsRequest listObjectsRequest = new ListObjectsRequest(bucketName);
              listObjectsRequest.setRequestPayer(payer);
              ObjectListing objectListing = ossClient.listObjects(listObjectsRequest);
              List<OSSObjectSummary> sums = objectListing.getObjectSummaries();
              for (OSSObjectSummary s : sums) {
                  System.out.println("\t" + s.getKey());
              }
  
              // Put object by specifying request payer
              String content1 = "hello";
              PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content1.getBytes()));
              putObjectRequest.setRequestPayer(payer);
              ossClient.putObject(putObjectRequest);
  
              // Get object by specifying request payer
              GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, objectName);
              getObjectRequest.setRequestPayer(payer);
              OSSObject ossObject = ossClient.getObject(getObjectRequest);
              ossObject.close();
  
              // Delete object by specifying request payer
              GenericRequest genericRequest = new GenericRequest(bucketName, objectName);
              genericRequest.setRequestPayer(payer);
              ossClient.deleteObject(genericRequest);
  
              // MultipartUpload object 
              // Initiate multipart upload by specifying request payer
              InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(bucketName, objectName);
              request.setRequestPayer(payer);
  
              InitiateMultipartUploadResult result = ossClient.initiateMultipartUpload(request);
              String uploadId = result.getUploadId();    
              List<PartETag> partETags =  new ArrayList<PartETag>();
  
              // Calc partCount
              final long partSize = 1 * 1024 * 1024L;   // 1MB
              final File sampleFile = new File("<yourLocalFileName>");
              long fileLength = sampleFile.length();
              int partCount = (int) (fileLength / partSize);
              if (fileLength % partSize != 0) {
                  partCount++;
              }
  
              // Upload part
              for (int i = 0; i < partCount; i++) {
                  long startPos = i * partSize;
                  long curPartSize = (i + 1 == partCount) ? (fileLength - startPos) : partSize;
                  InputStream instream = new FileInputStream(sampleFile);
                  instream.skip(startPos);
                  UploadPartRequest uploadPartRequest = new UploadPartRequest();
                  uploadPartRequest.setBucketName(bucketName);
                  uploadPartRequest.setKey(objectName);
                  uploadPartRequest.setUploadId(uploadId);
                  uploadPartRequest.setInputStream(instream);
                  uploadPartRequest.setPartSize(curPartSize);
                  uploadPartRequest.setPartNumber( i + 1);
                  // Upload part by specifying request payer
                  uploadPartRequest.setRequestPayer(payer);
                  UploadPartResult uploadPartResult = ossClient.uploadPart(uploadPartRequest);
                  partETags.add(uploadPartResult.getPartETag());
              }
  
              // Sort Etags by partNumber
              Collections.sort(partETags, new Comparator<PartETag>() {
                  public int compare(PartETag p1, PartETag p2) {
                      return p1.getPartNumber() - p2.getPartNumber();
                  }
              });
  
              // Complete multipart upload by specifying request payer 
              CompleteMultipartUploadRequest completeMultipartUploadRequest =
              		new CompleteMultipartUploadRequest(bucketName, objectName, uploadId, partETags);
              completeMultipartUploadRequest.setRequestPayer(payer);
  
              ossClient.completeMultipartUpload(completeMultipartUploadRequest);    
          } 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.");
              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 {
              /*
               * Do not forget to shut down the client finally to release all allocated resources.
               */
              ossClient.shutdown();
          }
      }
  
  }