How to use Aliyun OSS to store files and considerations

Source: Internet
Author: User
Tags documentation exception handling naming convention aliyun log4j
Development Process maven Dependencies
<dependency>
    <groupId>com.aliyun.oss</groupId>
    <artifactid>aliyun-sdk-oss</ artifactid>
    <version>2.7.0</version>
</dependency>
Instance Document
/** * Sample Description * * Hellooss is a sample program for OSS Java SDK that you can modify endpoint, Accesskeyid, Accesskeysecret, Bucketname, and run directly.
 * To run the method please refer to the README. * * This example does not include all features of the OSS Java SDK, detailed functionality and usage, see SDK manuals > JAVA-SDK, * link address: Https://help.aliyun.com/document_detail/oss
 /sdk/java-sdk/preface.html?spm=5176.docoss/sdk/java-sdk/.
 * When invoking a method of the OSS Java SDK, throwing an exception indicates an error occurred, and no exception was thrown to indicate successful execution. * When the error occurs, the OSS Java SDK method throws an exception, the exception includes error code, error message, please refer to "SDK manual > Java-sdk > Exception Handling", * link address is: https://help.aliyun.com/
 Document_detail/oss/sdk/java-sdk/exception.html?spm=5176.docoss/api-reference/error-response.
 * OSS Console can visually see the results of your call to the OSS Java SDK, the OSS console address is: https://oss.console.aliyun.com/index#/. * OSS Console usage Refer to the "Console User's Guide" in the Documentation Center and the link address is: https://help.aliyun.com/document_detail/oss/getting-started/
 Get-started.html?spm=5176.docoss/user_guide.
 * OSS's Document Center address is: https://help.aliyun.com/document_detail/oss/user_guide/overview.html. * The documentation address for the OSS Java SDK is: HTTPS://HELP.ALIYUN.COM/DOCUMENT_DETAIL/OSS/SDK/JAVA-SDK/INSTALL.HTML?SPM=5176.DOCOSS/SDK/java-sdk.

* * * */package Com.aliyun.oss.demo;
Import Java.io.BufferedReader;
Import Java.io.ByteArrayInputStream;
Import Java.io.File;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;

Import java.util.List;
Import com.aliyun.oss.ClientException;
Import com.aliyun.oss.OSSClient;
Import com.aliyun.oss.OSSException;
Import Com.aliyun.oss.model.BucketInfo;
Import Com.aliyun.oss.model.OSSObject;
Import Com.aliyun.oss.model.OSSObjectSummary;

Import com.aliyun.oss.model.ObjectListing;
Import Org.apache.log4j.Logger;

Import Org.apache.log4j.PropertyConfigurator;
        public class Hellooss {static Logger Logger = Logger.getlogger (Hellooss.class); Endpoint is the domain name to access OSS.
    If you have created a bucket on the OSS console, view the domain name on the console. If you have not yet created the Bucket,endpoint selection, see the Developer's Guide > Basic concepts > Access domain name in the Documentation Center,//link address is: Https://help.aliyun.com/document_detail/os S/user_guide/oss_concept/endpoint.html?spm=5176.docoss/user_guide/endpoint_region//Endpoint format as "http:// oss-cn-hangzhou.aliyuncs.com/", noteItaly http://after the bucket name,//such as "http://bucket-name.oss-cn-hangzhou.aliyuncs.com", is the wrong endpoint, please remove the "bucket-name".

    private static String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
    Accesskeyid and Accesskeysecret are OSS access keys that you can create and view on the console,//Create and view access keys by the link address: https://ak-console.aliyun.com/#/.
    Note: There are no spaces before and after Accesskeyid and Accesskeysecret, please check and remove extra spaces when copying from the console.
    private static String Accesskeyid = "<YourAccessKeyId>";

    private static String Accesskeysecret = "<YourAccessKeySecret>";
    Bucket is used to manage storage space for the storage object, see "Developer's Guide > Basic Concepts > OSS Basic Concepts" for more information.
    The bucket naming convention is as follows: only lowercase letters, numbers, and dashes (-) must begin with lowercase letters or numbers, and must be between 3-63 bytes in length.

    private static String Bucketname = "<YourBucketName>"; object is the basic unit of data that OSS stores, called OSS objects, also known as OSS files.
    See the "Developer's Guide > Basic concepts > OSS Basics" For a detailed description.
    The object naming convention is as follows: With UTF-8 encoding, the length must be between 1-1023 bytes and cannot begin with a "/" or "\" character.
private static String Firstkey = "My-first-key";
  /** * Execute upload operation * @param file * @param filename * @return * * Public String uplodeimages (InputStream file,string fileName) {try {//use Access to OSS ossclient
            . Putobject (Bucketname, filename,file);
        Returns the path to return path + fileName;
            catch (Ossexception e) {logger.error ("error Code:" + E. GetErrorCode ());
            Logger.error ("Request ID:" + E.getrequestid ());
            Logger.error ("Host ID:" + E.gethostid ()); Logger.error ("=====================>> file upload to Aliyun failed.")
            ");
        E.printstacktrace ();
                    catch (clientexception CE) {logger.error ("caught an clientexception, which means the client encountered" + "A serious internal problem while trying to communicate with OSS," + "such as N 
            OT being able to access the network. "); Logger.error ("=====================>> connection to the cloud failed.") 
        ");
    return null;
 }
}
error caused by


MAVEN Project Launch Web report: Java.lang.noclassdeffounderror:org/apache/http/ssl/truststrategy
wrong misunderstanding scheme httpclient Version Repeat

Aliyun-sdk-oss has httpclient version number 4.4.5
So we're going to remove our own dependencies or shield Aliyun from the dependent

Shielding Method

<dependency>  
    <groupId>com.aliyun.oss</groupId>  
    <artifactid>aliyun-sdk-oss</ artifactid>  
    <version>2.5.0</version>          
    <exclusions>   
        <exclusion>     
        < Groupid>org.apache.httpcomponents</groupid>   
        <artifactId>httpclient</artifactId>                      
        </exclusion>   
    </exclusions>   
</dependency>  
httpcomponents version too low
But it is possible that our version of 4.3.x, resulting in too low dependency, will be

the error test our httpcomponents minimum version of more than 4.4

Article from my blog-90 yards of farmers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.