List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client
@SdkInternalApi AmazonS3Client(AmazonS3ClientParams s3ClientParams)
From source file:itcr.gitsnes.MainActivity.java
License:Open Source License
/** The method sendGame makes many functions: * - Get all data from text boxes on layout add_game * - Makes a random bucket key for a photo/file and put the object into the bucket * - Wait for the success signal and send the JSON build from BackendHandler * to app-engine (Google)//from w ww .ja v a 2 s.c o m */ public void sendGame(View view) throws IOException { Toast.makeText(this, "Wait, we are uploading your game =) ", Toast.LENGTH_LONG); /* GET DATA FROM INTERFACE*/ EditText name = (EditText) this.findViewById(R.id.txt_name); EditText description = (EditText) this.findViewById(R.id.txt_desc); EditText category = (EditText) this.findViewById(R.id.txt_cat); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); /* GENERATE RANDOM KEYS FOR PHOTO AND FILE*/ this.file_key = "" + UUID.randomUUID().toString().replace("-", ""); this.image_key = "" + UUID.randomUUID().toString().replace("-", ""); /* SAVING GAME FILE/PHOTO ON AWS BUCKET*/ AmazonS3Client s3Client = new AmazonS3Client( new BasicAWSCredentials(KS.MY_ACCESS_KEY_ID, KS.MY_SECRET_KEY)); PutObjectRequest putObjectRequestnew = new PutObjectRequest(KS.BUCKET_NAME, this.file_key, this.s3game); putObjectRequestnew.setCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(putObjectRequestnew); PutObjectRequest putObjectImagenew = new PutObjectRequest(KS.BUCKET_IMG, this.image_key, this.s3image); putObjectImagenew.setCannedAcl(CannedAccessControlList.PublicRead); s3Client.putObject(putObjectImagenew); String actual_key = "none"; String actual_image = "none"; if (this.file_key != "none") actual_key = this.file_key; if (this.image_key != "none") actual_image = this.image_key; /* SEND JSON*/ new BackendHandler().sendJSON(KS.getCurrent_user(), name.getText().toString(), category.getText().toString(), description.getText().toString(), actual_image, actual_key); Log.i(TAG, "Successful JSON send"); Toast.makeText(this, "Congratulations your game has been sent", Toast.LENGTH_LONG); }
From source file:Java21.S3Files.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from w w w . j a v a 2s . c o m*/ * The ProfileCredentialsProvider will return your [default] * credential profile by reading from the credentials file located at * (~/.aws/credentials). */ AWSCredentials credentials = null; try { credentials = new ProfileCredentialsProvider().getCredentials(); } catch (Exception e) { throw new AmazonClientException("Cannot load the credentials from the credential profiles file. " + "Please make sure that your credentials file is at the correct " + "location (~/.aws/credentials), and is in valid format.", e); } AmazonS3 s3 = new AmazonS3Client(credentials); Region usWest2 = Region.getRegion(Regions.US_WEST_2); s3.setRegion(usWest2); String bucketName = "msm-gb-env-etl-iq/dev1-dwh/" + UUID.randomUUID(); //String key = ""; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); /* * List objects in your bucket by prefix - There are many options for * listing the objects in your bucket. Keep in mind that buckets with * many objects might truncate their results when listing their objects, * so be sure to check if the returned object listing is truncated, and * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve * additional results. */ System.out.println("Listing objects"); ObjectListing objectListing = s3 .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My")); for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) { System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")"); } System.out.println(); }
From source file:jp.buyee.glover.KinesisConnectorExecutor.java
License:Open Source License
/** * Helper method to create the Amazon S3 bucket. * // w w w . java 2 s .com * @param s3Bucket * The name of the bucket to create */ private void createS3Bucket(String s3Bucket) { AmazonS3Client client = new AmazonS3Client(config.AWS_CREDENTIALS_PROVIDER); client.setEndpoint(config.S3_ENDPOINT); LOG.info("Creating Amazon S3 bucket " + s3Bucket); S3Utils.createBucket(client, s3Bucket); }
From source file:jp.dqneo.amazons3.sample.S3Sample.java
License:Open Source License
public static void main(String[] args) throws IOException { /*/*from ww w . j av a 2s. co m*/ * Important: Be sure to fill in your AWS access credentials in the * AwsCredentials.properties file before you try to run this * sample. * http://aws.amazon.com/security-credentials */ AmazonS3 s3 = new AmazonS3Client( new PropertiesCredentials(S3Sample.class.getResourceAsStream("AwsCredentials.properties"))); s3.setEndpoint("https://s3-ap-northeast-1.amazonaws.com"); String bucketName = "my-first-s3-bucket-" + UUID.randomUUID(); String key = "2/foo/bar"; System.out.println("==========================================="); System.out.println("Getting Started with Amazon S3"); System.out.println("===========================================\n"); try { /* * Upload an object to your bucket - You can easily upload a file to * S3, or upload directly an InputStream if you know the length of * the data in the stream. You can also specify your own metadata * when uploading to S3, which allows you set a variety of options * like content-type and content-encoding, plus additional metadata * specific to your applications. */ System.out.println("Uploading a new object to S3 from a file\n"); PutObjectRequest p = new PutObjectRequest(bucketName, key, createSampleFile()); s3.putObject(p); System.out.println("ok\n"); } catch (AmazonServiceException ase) { System.out.println("Caught an AmazonServiceException, which means your request made it " + "to Amazon S3, but was rejected with an error response for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which means the client encountered " + "a serious internal problem while trying to communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); } }
From source file:littleware.apps.fishRunner.FishModule.java
License:LGPL
@Override public void configure(Binder binder) { // setup AWS ... final AWSCredentials creds = new BasicAWSCredentials(s3Key, s3Secret); binder.bind(AWSCredentials.class).toInstance(creds); binder.bind(AmazonS3.class).to(AmazonS3Client.class); binder.bind(AmazonS3Client.class).toInstance(new AmazonS3Client(creds)); binder.bind(java.net.URI.class).annotatedWith(Names.named("DATABASE_URL")).toInstance(dbURI); binder.bind(GlassFish.class).toProvider(FishFactory.class).in(Scopes.SINGLETON); binder.bind(FishFactory.class).in(Scopes.SINGLETON); final GlassFishProperties glassfishProperties = new GlassFishProperties(); glassfishProperties.setPort("http-listener", glassfishPort); // glassfishProperties.setPort("https-listener", 8181); binder.bind(GlassFishProperties.class).toInstance(glassfishProperties); }
From source file:localdomain.localhost.AmazonS3Resources.java
License:Apache License
/** * @param accessKey//from w w w.j a v a2s.co m * @param secretKey * @param bucketName * @throws AmazonServiceException */ public AmazonS3Resources(String accessKey, String secretKey, String bucketName) throws AmazonServiceException { AWSCredentials credentials; credentials = new BasicAWSCredentials(accessKey, secretKey); amazonS3Client = new AmazonS3Client(credentials); if (!amazonS3Client.doesBucketExist(bucketName)) { throw new IllegalArgumentException("Bucket " + bucketName + " does not exist"); } this.bucketName = bucketName; }
From source file:mail.server.storage.AWSStorageCreation.java
License:GNU General Public License
public Map<String, String> create(String email, String region) throws Exception { log.debug("I will now figure out what region to put things in", region); Region awsRegion = Region.valueOf(region); String awsRegionString = awsRegion.toString(); if (awsRegionString == null) awsRegionString = ""; String awsRegionStringEndPoint = awsRegionString.isEmpty() ? "s3.amazonaws.com" : ("s3-" + awsRegionString + ".amazonaws.com"); log.debug("I will now log in to S3 and the IdentityManagement to check these credentials."); SimpleAWSCredentials credentials = new SimpleAWSCredentials(awsAccessKeyId, awsSecretKey); AmazonS3 s3 = new AmazonS3Client(credentials); AmazonIdentityManagement im = new AmazonIdentityManagementClient(credentials); log.debug("Successfully logged into S3"); log.debug("I will now derive names for items"); deriveNames(generateBucketName(email)); log.debug("I will now try to:\n" + " 1. Create the S3 Bucket with name ", bucketName, "\n" + " 2. Create two IAM Identities for permissions -\n" + " ", writeIdentity, " to be sent to the mail server to be able to write to the mailbox.\n" + " ", writeIdentity, " to be stored in your configuration to enable the mail client to read and write mail.\n\n"); s3.setEndpoint(awsRegionStringEndPoint); s3.createBucket(bucketName, awsRegion); log.debug("Setting website configuration"); BucketWebsiteConfiguration bwc = new BucketWebsiteConfiguration("index.html"); s3.setBucketWebsiteConfiguration(bucketName, bwc); log.debug("Done"); log.debug("Enabling CORS"); CORSRule rule1 = new CORSRule().withId("CORSRule1") .withAllowedMethods(Arrays.asList(new CORSRule.AllowedMethods[] { CORSRule.AllowedMethods.GET, CORSRule.AllowedMethods.PUT, CORSRule.AllowedMethods.DELETE })) .withAllowedOrigins(Arrays.asList(new String[] { "*" })).withMaxAgeSeconds(3000) .withAllowedHeaders(Arrays.asList(new String[] { "*" })) .withExposedHeaders(Arrays.asList(new String[] { "ETag" })); BucketCrossOriginConfiguration cors = new BucketCrossOriginConfiguration(); cors.setRules(Arrays.asList(new CORSRule[] { rule1 })); s3.setBucketCrossOriginConfiguration(bucketName, cors); log.debug("Done"); log.format("Creating group %s ... ", groupName); im.createGroup(new CreateGroupRequest().withGroupName(groupName)); log.debug("Done"); log.format("Creating user %s ... ", writeIdentity); im.createUser(new CreateUserRequest().withUserName(writeIdentity)); log.debug("Done"); log.format("Adding user %s to group %s ... ", writeIdentity, groupName); im.addUserToGroup(new AddUserToGroupRequest().withGroupName(groupName).withUserName(writeIdentity)); log.debug("Done"); log.format("Creating user %s ... ", readWriteIdentity); im.createUser(new CreateUserRequest().withUserName(readWriteIdentity)); log.debug("Done"); log.format("Adding user %s to group %s ... ", readWriteIdentity, groupName); im.addUserToGroup(new AddUserToGroupRequest().withGroupName(groupName).withUserName(readWriteIdentity)); log.debug("Done"); log.format("Creating permissions for %s to write to bucket %s ... \n", writeIdentity, bucketName); String writePolicyRaw = "{ \n" + " #Statement#: [ \n" + " { \n" + " #Sid#: #SID#, \n" + " #Action#: [ \n" + " #s3:PutObject#, \n" + " #s3:PutObjectAcl# \n" + " ], \n" + " #Effect#: #Allow#, \n" + " #Resource#: [ \n" + " #arn:aws:s3:::BUCKET/*#\n" + " ] \n" + " } \n" + " ] \n" + "}\n"; String writePolicy = writePolicyRaw.replaceAll("#", "\"").replace("SID", policyWriteName).replace("BUCKET", bucketName);/* w w w. j av a2 s . c o m*/ // q.println ("Policy definition: " + writePolicy); im.putUserPolicy(new PutUserPolicyRequest().withUserName(writeIdentity).withPolicyDocument(writePolicy) .withPolicyName(policyWriteName)); log.debug("Done"); log.format("Creating permissions for %s to read/write to bucket %s ... \n", writeIdentity, bucketName); String readWritePolicyRaw = "{ \n" + " #Statement#: [ \n" + " { \n" + " #Sid#: #SID#, \n" + " #Action#: [ \n" + " #s3:PutObject#, \n" + " #s3:PutObjectAcl#, \n" + " #s3:DeleteObject#, \n" + " #s3:Get*#, \n" + " #s3:List*# \n" + " ], \n" + " #Effect#: #Allow#, \n" + " #Resource#: [ \n" + " #arn:aws:s3:::BUCKET/*#,\n" + " #arn:aws:s3:::BUCKET# \n" + " ] \n" + " } \n" + " ] \n" + "}\n"; String readWritePolicy = readWritePolicyRaw.replaceAll("#", "\"").replace("SID", policyReadWriteName) .replace("BUCKET", bucketName); // q.println ("Policy definition: " + readPolicy); im.putUserPolicy(new PutUserPolicyRequest().withUserName(readWriteIdentity) .withPolicyDocument(readWritePolicy).withPolicyName(policyReadWriteName)); log.debug("Done"); log.format("Requesting access key for %s", writeIdentity); writeAccessKey = im.createAccessKey(new CreateAccessKeyRequest().withUserName(writeIdentity)) .getAccessKey(); log.format("Received [%s] [%s] Done.\n", writeAccessKey.getAccessKeyId(), writeAccessKey.getSecretAccessKey()); log.format("Requesting access key for %s", readWriteIdentity); readWriteAccessKey = im.createAccessKey(new CreateAccessKeyRequest().withUserName(readWriteIdentity)) .getAccessKey(); log.format("Received [%s] [%s] Done.\n", readWriteAccessKey.getAccessKeyId(), readWriteAccessKey.getSecretAccessKey()); log.debug(); log.debug("I have finished the creating the S3 items.\n"); return Maps.toMap("bucketName", bucketName, "bucketRegion", awsRegionString, "writeAccessKey", writeAccessKey.getAccessKeyId(), "writeSecretKey", writeAccessKey.getSecretAccessKey(), "readWriteAccessKey", readWriteAccessKey.getAccessKeyId(), "readWriteSecretKey", readWriteAccessKey.getSecretAccessKey()); }
From source file:mail.server.storage.AWSStorageDelete.java
License:GNU General Public License
public void delete(String bucketName, String awsAccessKeyId, String awsSecretKey) throws Exception { log.debug("will delete", bucketName); SimpleAWSCredentials credentials = new SimpleAWSCredentials(awsAccessKeyId, awsSecretKey); AmazonS3 s3 = new AmazonS3Client(credentials); AmazonIdentityManagement im = new AmazonIdentityManagementClient(credentials); log.debug("deriving names"); deriveNames(bucketName);//from ww w. ja v a 2 s . co m deleteUser(im, groupName, readWriteIdentity, policyReadWriteName, false); deleteUser(im, groupName, writeIdentity, policyWriteName, false); log.debug("deleting group", groupName); im.deleteGroup(new DeleteGroupRequest().withGroupName(groupName)); deleteBucketContents(s3, bucketName); log.debug("deleting bucket"); s3.deleteBucket(new DeleteBucketRequest(bucketName)); }
From source file:md.djembe.aws.AmazonS3WebClient.java
License:Apache License
public static AmazonS3 getS3Client() { ACCESS_KEY = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_ACCESS_KEY); SECRET_KEY = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_SECRET_KEY); BUCKET_NAME = ApplicationCache.getEntry(WebAppProperties.SETTINGS, WebAppProperties.S3_BUCKET_NAME); BasicAWSCredentials awsCredentials = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY); AmazonS3 amazonS3 = new AmazonS3Client(awsCredentials); return amazonS3; }
From source file:me.footlights.server.AmazonUploader.java
License:Apache License
@Inject public AmazonUploader(Preferences preferences) { this.prefs = preferences; this.authSecret = getMandatoryPreference("cas.secret"); final String keyId = getMandatoryPreference("amazon.keyId"); final String secret = getMandatoryPreference("amazon.secretKey"); if (keyId.isEmpty()) throw new ConfigurationError("Amazon key ID not set"); if (secret.isEmpty()) throw new ConfigurationError("Amazon secret key not set"); AWSCredentials cred = new AWSCredentials() { @Override// w w w . ja v a2s.co m public String getAWSAccessKeyId() { return keyId; } @Override public String getAWSSecretKey() { return secret; } }; s3 = new AmazonS3Client(cred); }