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.IOException;
  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.ObjectMetadata;
  
  /**
   * Examples about metadata information get and set.
   * 
   */
  public class ObjectMetaSample {
      
      private static String endpoint = "<endpoint, http://oss-cn-hangzhou.aliyuncs.com>";
      private static String accessKeyId = "<accessKeyId>";
      private static String accessKeySecret = "<accessKeySecret>";
      private static String bucketName = "<bucketName>";
      private static String key = "<key>";
      private static String content = "Hello OSS";
      
      
      public static void main(String[] args) throws IOException {
          /*
           * Constructs a client instance with your account for accessing OSS
           */
          OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                  
          try {
              ObjectMetadata meta = new ObjectMetadata();
              
              // Sets the content type.
              meta.setContentType("text/plain");
              // Sets the MD5 data---please update it with the actual value.
              meta.setContentMD5("");
              // Sets the custom metadata.
              meta.addUserMetadata("meta", "meta-value");
              
              // Uploads the file
              ossClient.putObject(bucketName, key, 
                      new ByteArrayInputStream(content.getBytes()), meta);
             
              // Gets the object metadata information.
              ObjectMetadata metadata = ossClient.getObjectMetadata(bucketName, key);
              System.out.println(metadata.getContentType());
              System.out.println(metadata.getLastModified());
              System.out.println(metadata.getUserMetadata().get("meta")); 
              
              ossClient.deleteObject(bucketName, key);
              
          } 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();
          }
      }
      
  }
  