AmazonS3 for Java

Source: Internet
Author: User
Tags soap aws sdk wsdl

Official Document: Https://docs.aws.amazon.com/zh_cn/sdk-for-java/v1/developer-guide/welcome.html

Thank the Great God for sharing 53097459

Amazon S3 is a storage server on the cloud that functions only for storage. Unlike the normal server, it does not have a user interface, and even the usual SSH command login function is not. One way to deal with it is to use its management interface and the other with its programming interface.

Amazon S3 a minimal set of features focused on simplicity and robustness is built in. Here are Some of the benefits of Amazon S3 services:

Create Buckets – Create and name buckets that store data. Buckets are the underlying container for data storage in Amazon S3.

Store data in buckets – store an unlimited amount of data in a bucket. You can upload as many objects as you want to an Amazon S3 bucket. Each data element can contain up to 5 terabytes of data. Each object is stored and retrieved using a developer-assigned unique key.

Download Data – Download your data or allow others to download it. Download your data at any time or allow others to perform the same action.

Permissions -You can grant access or deny access to other people who want to upload or download data in your Amazon S3 bucket. Grants upload and download licenses to three types of users. Authentication mechanisms help ensure that data is secured against unauthorized access.

Standard interface – using standard-based REST and SOAP interfaces, they can be used with any Internet development kit.

Note
SOAP support on HTTP is deprecated, but can still be used on HTTPS. New Amazon S3 features will not be supported for SOAP. We recommend that you use the REST API or the AWS SDK.

What is Amazon S3?

Installing the AWS CLI using the MSI installer (Windows)

Nouns and terminology
AWSAccount: Amazon Web Service Accounts, a company that typically applies for an account that can be considered an administrator.

iam User: General AWS has only a handful of people, and in order to operate, adding sub accounts is called an IAM account, with different permissions for each account. Both AWS and IAM accounts have an access key ID and a secret key. The account name is used when logging on to the Amazon Web management interface, while the programming interface uses access key ID and secret key, where the ID is 16 characters and secret is 40 bytes.

Storage Buckets

Buckets are containers used in Amazon S3 to store the data elements. Each of the data elements is stored in a bucket. For example, if the data element named Photos/puppy.jpg is stored in a johnsmith bucket, you can use a URL http://johnsmith.s3.amazonaws.com/photos/puppy.jpg Addressing this data element

Buckets have several uses: organize the highest level of Amazon S3 namespaces, identify accounts responsible for storage and data transfer charges, play a role in access control, and use as a summary unit for usage reporting.

The object on Access CONTROL:S3 is not publicly accessible by default, which means that you upload an object key=photos/puppy.jpg and cannot be directly passed HTTP// Johnsmith.s3.amazonaws.com/photos/puppy.jpg Direct Access, you will be prompted for access Denied as long as you have set permissions (ACL or policy).

Object

An object is a basic entity stored in Amazon S3. The data element consists of data metadata and metadata. The data section is opaque to Amazon S3. Metadata is a set of name-value pairs that describe the data elements. This includes some default metadata, such as the last modified date, and standard HTTP metadata (such as Content-type). You can also specify custom metadata when you store the object.

In a bucket, the data element is uniquely identified by the key (name) and the version ID. For more information, see Key and version control.

Key

A key is a unique identifier for the data element in a bucket. Each data element within a bucket can have only one key. By combining buckets, keys, and version IDs to uniquely identify each data element, Amazon S3 is treated as a basic data mapping between bucket + key + version and the entity itself. Combine Web service endpoints, bucket names, keys, and versions (optional) to uniquely address each of the data elements in Amazon S3. For example, in URL http://doc.s3.amazonaws.com/2006-03-01/AmazonS3.wsdl , "Doc" is the name of the bucket, and "2006-03-01/amazons3.wsdl" is the key.

Common operations

Select the AWS SDK

Jar package in all folders after decompression

Introduction to here, Direct sticker Code

//Declaration Variables    /*** KeyID*/     Public StaticString Accesskeyid = "Accesskeyid"; /*** Key*/     Public StaticString Secretkey = "Secretkey"; /*** To understand as a large magnetic storage hard disk, its capacity in T units*/     Public StaticString bucketname = "Bucketname"; /*** Upload file address*/     Public StaticString LocalPath = "e:xxx/xxx. JPG "; /*** keys for storing new objects*/     Public StaticString S3path = "AmazonS3"; //Create an Amazon S3 objectawscredentials Credentials;    AmazonS3 s3client; Credentials=Newbasicawscredentials (Accesskeyid, Secretkey); S3client=Newamazons3client (credentials); //Set AreaRegion cnnorth1 =region.getregion (regions.cn_north_1);    S3client.setregion (CNNORTH1); /*** View all available buckets *@params3client*/     Public Static voidGetallbucket (AmazonS3 s3client) {List<Bucket> buckets =s3client.listbuckets ();  for(Bucket bucket:buckets) {System.out.println ("Bucket:" +bucket.getname ()); }    }    /*** View all objects under the bucket *@parambucketname Buckets*/     Public Static voidGetallbucketobject (AmazonS3 s3client) {objectlisting objects=s3client.listobjects (bucketname);  Do {             for(S3objectsummary objectSummary:objects.getObjectSummaries ()) {System.out.println ("Object:" +Objectsummary.getkey ()); } Objects=s3client.listnextbatchofobjects (objects); }  while(objects.istruncated ()); }    /*** amazonS3 File Upload *@paramS3client *@paramBucketname Save to a bucket *@paramkey to save the file (saved in key-value form) *@paramFile Upload Files*/     Public Static voidamazons3upload (AmazonS3 s3client,string bucketname,string key,file File) {putobjectresult result=s3client.putobject (Newputobjectrequest (bucketname, key, file)); System.out.println ("Putobjectresult:" +result.tostring ()); }    /*** amazonS3 File Download *@paramS3client *@parambucketname Download data for a bucket *@paramkey to download the file key *@paramtargetfilepath Download File Save address*/     Public Static voidamazons3downloading (AmazonS3 s3client,string bucketname,string key,string targetfilepath) {S3Object object = S3client.getobject (Newgetobjectrequest (Bucketname,key)); if(object!=NULL) {System.out.println ("Content-type:" +object.getobjectmetadata (). getContentType ()); InputStream input=NULL; FileOutputStream FileOutputStream=NULL; byte[] data =NULL; Try {                //Get file Streaminput=object.getobjectcontent (); Data=New byte[Input.available ()]; intLen = 0; FileOutputStream=NewFileOutputStream (Targetfilepath);  while(len = input.read (data))! =-1) {fileoutputstream.write (data,0, Len); }            } Catch(IOException e) {e.printstacktrace (); }finally{                if(fileoutputstream!=NULL){                    Try{fileoutputstream.close (); } Catch(IOException e) {e.printstacktrace (); }                }                if(input!=NULL){                    Try{input.close (); } Catch(IOException e) {e.printstacktrace (); }                }            }        }    }    /*** File deletion *@paramS3client *@parambucketname Delete a bucket of files *@paramKey Delete file key*/     Public Static voidamazons3deleteobject (AmazonS3 s3client,string bucketname,string key) {S3client.deleteobject (bucketname, key); }    /*** Delete Bucket *@paramS3client *@paramBucketname buckets that need to be deleted*/     Public Static voidamazons3deletebucket (AmazonS3 s3client,string bucketname) {s3client.deletebucket (bucketname); }    //when the file is relatively large, it is necessary to use multi-threaded upload. (not tested)Transfermanager TM =NewTransfermanager (s3client); Transfermanagerconfiguration conf=tm.getconfiguration (); intThreshold = 4 * 1024 * 1024;      Conf.setmultipartuploadthreshold (threshold);      Tm.setconfiguration (conf); Upload Upload= Tm.upload (Bucketname, S3path,NewFile (LocalPath)); Transferprogress P=upload.getprogress ();  while(Upload.isdone () = =false)      {                     intPercent = (int) (p.getpercenttransfered ()); Thread.Sleep (500); }       //add public permissions by defaultS3client.setobjectacl (Bucketname, S3path, Cannedaccesscontrollist.publicread);

    • 155
    • 156
    • 157

I also just used, not detailed place, please see in the API

AmazonS3 for Java

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.