Using Java Development aliyun OSS Open cloud storage services

Source: Internet
Author: User

After purchasing the Aliyun, we found that the general ECS storage space is relatively small, of course, we can dynamically expand some of the storage space. But as developers, we need some more flexibility in our day-to-day use, so we need to have some of our own extensions to OSS storage. Let's share with you today how to use Java to butt Aliyun OSS storage services. OSS Product Overview

Aliyun Object Storage Service (object Storage services, referred to as OSS) is a massive, secure, low-cost, and reliable cloud storage service provided by Aliyun. You can upload and download data at any application, any time, any place by invoking the API, or you can simply manage the data through the WEB console. OSS is suitable for storing any type of file, suitable for various websites, development enterprises and developers.
Open Aliyun OSS service

First you have to log on to the Aliyun website, and then select the product--> stored in the Cdn--> object storage OSS. The following figure:

Then we can open it according to our own situation. Once the opening is complete, we can enter the OSS console. OSS Console

The OSS console layout includes bucket lists, bucket locations, creation times, and bucket operations. Bucket This concept in a more popular parlance, is the user created top-level directory, all users uploaded files must exist under bucket, and bucket in the OSS system is not the same name. If a user has already created the bucket name ABC, then unfortunately, you can't create a bucket named ABC, each user can create 10 bucket. Note the red Circle accesskey Management button, click on the AccessKey management interface can be.

Click on the display after entering, you can see the secret key, the use of Accesskeyid&accesskeysecret see OSS API documentation
Click on the specific bucket to enter the bucket management functions, including bucket property settings, Object management.

Create bucket

If you have not yet created bucket, click New Bucket.

Enter the new bucket page and fill in the bucket information

Fill in the Bucket name, select the domain, set read and write permissions. After completing the bucket information, click Submit to create the bucket.
Uploading Files

Click on the red circle callout upload file, will pop up the file box list, select the file upload to upload.

Share Files

Click on the corresponding file to get the address, will pop up an object address information, where the address link can be accessed in the public network. Others click on this link can download to this file, but pro this is to pay attention to generate traffic costs.

Note: If your OSS bucket an unbound domain name, you can download the file by default when you access the file link, and you cannot browse the file directly.
Java Development OSS Storage Services Open OSS Server, there will be endpoint, with the address; Accesskeyid, similar to user name; Accesskeysecret, similar to password; Buketname: first-level folder name, A user can create 10 bucketname, similar to our c,d,e,f letter; Accessurl, it is obvious that the user can access the address through the browser. For example, if we upload an HTML file, we can access it via the "accessurl/bucketname/directory/filename. html." I'm here responsible for a page static function, the dynamic JSP page generated static HTML page, and then upload the OSS server, return address, let users directly access the static page, so that the speed of access greatly faster.
OSS Information entity class,


Import java.util.Properties;
	public class Ossconfigure {private String endpoint;
	Private String Accesskeyid;
	Private String Accesskeysecret;
	Private String Bucketname;

	Private String Accessurl;
	 The public ossconfigure () {}/** * gets through the configuration file. properties file.

		* @param storageconfname * @throws ioexception/Public ossconfigure (String storageconfname) throws IOException {
		Properties prop = new properties ();

		Prop.load (This.getclass (). getClassLoader (). getResourceAsStream ("conf/" + storageconfname));
		Endpoint = Prop.getproperty ("endpoint"). Trim ();
		Accesskeyid = Prop.getproperty ("Accesskeyid"). Trim ();
		Accesskeysecret = Prop.getproperty ("Accesskeysecret"). Trim ();
		Bucketname = Prop.getproperty ("Bucketname"). Trim ();

	Accessurl = Prop.getproperty ("Accessurl"). Trim (); Public Ossconfigure (String endpoint, String Accesskeyid, String Accesskeysecret, String bucketname, String Accessur
		L) {this.endpoint = endpoint; This. Accesskeyid = Accesskeyid;
		This.accesskeysecret = Accesskeysecret;
		This.bucketname = Bucketname;
	This.accessurl = Accessurl;
	Public String Getendpoint () {return endpoint;
	} public void setEndPoint (String endpoint) {this.endpoint = endpoint;
	Public String Getaccesskeyid () {return accesskeyid;
	} public void Setaccesskeyid (String accesskeyid) {This.accesskeyid = Accesskeyid;
	Public String Getaccesskeysecret () {return accesskeysecret;
	} public void Setaccesskeysecret (String accesskeysecret) {this.accesskeysecret = Accesskeysecret;
	Public String Getbucketname () {return bucketname;
	} public void Setbucketname (String bucketname) {this.bucketname = Bucketname;
	Public String Getaccessurl () {return accessurl;
	} public void Setaccessurl (String accessurl) {this.accessurl = Accessurl; }

Then create a tool class, where you need to put forward the OSS server, all of them are objects, no folder concept, upload files, directly write road strength, he will automatically create. There is an entrance ossclient, and many methods are implemented through it.


Import Org.springframework.web.multipart.MultipartFile;
Import com.aliyun.oss.OSSClient;
Import com.aliyun.oss.model.ObjectListing;

Import Com.aliyun.oss.model.ObjectMetadata; 
	/** * to upload and delete OSS server * */public class Ossmanageutil {/** * upload OSS Server file * @Title: UploadFile * @Description: * @param @param ossconfigure * @param @param file * @param @param remotepath * @param @return * @param Except Ion Set File * @return String return type * @throws */public static String UploadFile (Ossconfigure ossconfigure,file fi
		Le,string RemotePath) throws exception{InputStream Filecontent=null;
		Filecontent=new fileinputstream (file); Ossclient ossclient=new ossclient (Ossconfigure.getendpoint (), Ossconfigure.getaccesskeyid (),
		 Ossconfigure.getaccesskeysecret ()); String Remotefilepath = remotepath.substring (0, Remotepath.length ()). ReplaceAll ("\\\\","/")+"/";
		Create the metadata objectmetadata objectmetadata=new objectmetadata () to upload the object;
		Objectmetadata.setcontentlength (Filecontent.available ());
		Objectmetadata.setcachecontrol ("No-cache");
		Objectmetadata.setheader ("Pragma", "No-cache");
		Objectmetadata.setcontenttype (ContentType (File.getname (). substring (File.getname (). LastIndexOf ("."));
		Objectmetadata.setcontentdisposition ("inline;filename=" + file.getname ()); Upload file Ossclient.putobject (ossconfigure.getbucketname (), Remotefilepath + file.getname (), Filecontent,
		System.out.println (Ossconfigure.getaccessurl () + "/" +remotefilepath + file.getname ());
	return Ossconfigure.getaccessurl () + "/" +remotefilepath + file.getname (); /** * Delete files on OSS server under Key * @Title: DeleteFile * @Description: * @param @param ossconfigure * @param @param f Ilepath Set file * @return void return type * @throws/public static void DeleteFile (Ossconfigure ossconfigure,string FilePath) {ossclient ossclient = new OSSclient (Ossconfigure.getendpoint (), Ossconfigure.getaccesskeyid (), Ossconfigure.getaccesskeysecret ());
	Ossclient.deleteobject (Ossconfigure.getbucketname (), FilePath); /** * Description: Judge ContentType * @Version1.0 * @param filenameextension file suffix * for OSS service file uploads * @ Return String */public static string ContentType (String filenameextension) {if (Filenameextension.equals ("BMP") | |
		Filenameextension.equals ("BMP")) {return "image/bmp";} if (Filenameextension.equals ("GIF") | |
		Filenameextension.equals ("gif")) {return "Image/gif";} if (Filenameextension.equals ("JPEG") | |
		   Filenameextension.equals ("JPEG") | | Filenameextension.equals ("JPG") | |	
		   Filenameextension.equals ("jpg") | | Filenameextension.equals ("PNG") | |
		Filenameextension.equals ("PNG")) {return "Image/jpeg";} if (Filenameextension.equals ("HTML") | |
		Filenameextension.equals ("html")) {return "text/html";} if (Filenameextension.equals ("TXT") | |
		Filenameextension.equals ("TXT")) {return "Text/plain";} if (filenAmeextension.equals ("VSD") | |
		Filenameextension.equals ("VSD")) {return "Application/vnd.visio";} if (Filenameextension.equals ("PPTX") | |
			Filenameextension.equals ("pptx") | | Filenameextension.equals ("PPT") | |
		Filenameextension.equals ("ppt") {return "Application/";} if (Filenameextension.equals ("DOCX") | |
			Filenameextension.equals ("docx") | | Filenameextension.equals ("DOC") | |
		Filenameextension.equals ("Doc")) {return "Application/msword";} if (Filenameextension.equals ("XML") | |
		Filenameextension.equals ("xml")) {return "Text/xml";}
	 return "text/html"; }
Okay, here's the simple development of the OSS that's done. Because of the time, I write here, everything is difficult at the beginning, you have a simple understanding of the OSS. The rest of the API on the website is a piece of cake for you. Now I'm talking about the problems and solutions that are encountered during use.
problems and solutions encountered using OSS storage services ECS Server is cleanedWhen the server traffic reaches the user cloud Shield set cleaning threshold, will automatically enter the flow cleaning.
Please note that because the security policy does not do 100% identify the request is malicious, so the server may have a low probability of manslaughter after cleaning, but in most cases the normal request will not be affected, if the suspicion of the wrong kill can be submitted to work orders by the After-sale support to help judge.
ECS Server Intranet in different accountBy default, the server intranet under different accounts is not. If the user has multiple accounts at the same time, and multiple accounts under the server, then these servers intranet is not a pass. If you need a different account under the intranet interoperability, need to submit a work order contact after sale, Aliyun after the sale of engineers will help to get through the intranet.
Please note that the need for the intranet to get through the different accounts need to submit all the work orders, which IP and which IP for intranet interoperability, or account of all the server intranet interoperability. And in each work list, the network has been submitted to exchange the number of work list (except for their own work number), in order to Aliyun after the After-sale engineer for security verification.
OSS Memory LeaksIf you encounter an OSS memory leak problem, first check your code, and certainly some of the IO stream is not closed. If not resolved in time, you can submit work orders, contact customer service. callback of OSS return data format non-JSON formatIt is possible that an exception was thrown in the application server process, resulting in a failure to return to the OSS as expected, instead of a return of some stack information, which could result in non-JSON-formatted data generation.
OSS return status is not a400 or other status such as 404/403 is the application server returned to the OSS HTTP is 400 or 404/403, and normally the application server must return 200 to the OSS. 502 is because the application server does not have a Web service at all, and does not listen for callback requests from OSS.
Related Article

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: