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.BufferedReader;
  import java.io.ByteArrayInputStream;
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.io.Writer;
  
  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.GetObjectRequest;
  import com.aliyun.oss.model.OSSObject;
  import com.aliyun.oss.model.PutObjectRequest;
  
  /**
   * This sample demonstrates how to upload/download an object to/from 
   * Aliyun OSS using the OSS SDK for Java.
   */
  public class SimpleGetObjectSample {
      
      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 key = "*** Provide key ***";
      
      public static void main(String[] args) throws IOException {
          /*
           * Constructs a client instance with your account for accessing OSS
           */
          OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
          
          try {
              
              /**
               * Note that there are two ways of uploading an object to your bucket, the one 
               * by specifying an input stream as content source, the other by specifying a file.
               */
              
              /*
               * Upload an object to your bucket from an input stream
               */
              System.out.println("Uploading a new object to OSS from an input stream\n");
              String content = "Thank you for using Aliyun Object Storage Service";
              client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes()));
              
              /*
               * Upload an object to your bucket from a file
               */
              System.out.println("Uploading a new object to OSS from a file\n");
              client.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));
              
              /*
               * Download an object from your bucket
               */
              System.out.println("Downloading an object");
              OSSObject object = client.getObject(new GetObjectRequest(bucketName, key));
              System.out.println("Content-Type: "  + object.getObjectMetadata().getContentType());
              displayTextInputStream(object.getObjectContent());
              
          } 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.
               */
              client.shutdown();
          }
      }
      
      private static File createSampleFile() throws IOException {
          File file = File.createTempFile("oss-java-sdk-", ".txt");
          file.deleteOnExit();
  
          Writer writer = new OutputStreamWriter(new FileOutputStream(file));
          writer.write("abcdefghijklmnopqrstuvwxyz\n");
          writer.write("0123456789011234567890\n");
          writer.close();
  
          return file;
      }
  
      private static void displayTextInputStream(InputStream input) throws IOException {
          BufferedReader reader = new BufferedReader(new InputStreamReader(input));
          while (true) {
              String line = reader.readLine();
              if (line == null) break;
  
              System.out.println("\t" + line);
          }
          System.out.println();
  
          reader.close();
      }
      
  }
  