Openshift is DevOps platform of Red Hat. My personal idea about Openshift is that it is simple and will provide you more efficiency (for especially startups)
Openshift does't provide you a storage like S3 and in my project I need to access Amazon S3 storage from my Openshift application.
Steps, you have to apply ;
(Assumption : You have already installed rhc client for openshift.)
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.2</version>
</dependency>
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
/**
* Servlet implementation class S3EventListener
*/
@WebServlet("/S3EventListener")
public class S3EventListener extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public S3EventListener() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Get Method is not supported.");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AmazonS3 s3client = new AmazonS3Client(new EnvironmentVariableCredentialsProvider());
try {
System.out.println("Uploading a new object to S3 from a post\n");
BufferedReader reader = request.getReader();
ByteArrayInputStream baiStream = new ByteArrayInputStream(reader.readLine().getBytes());
s3client.putObject(new PutObjectRequest(
"sample-bucket", "sample-file", baiStream,new ObjectMetadata() ));
} 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 " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
doGet(request, response);
}
}
http://docs.aws.amazon.com/AWSSdkDocsJava/latest//DeveloperGuide/credentials.html
Openshift does't provide you a storage like S3 and in my project I need to access Amazon S3 storage from my Openshift application.
- I have an openshift application that is placed at Amazon US-East.
- I have S3 buckets under my Amazon account.
Steps, you have to apply ;
(Assumption : You have already installed rhc client for openshift.)
- Define a Group for S3 access on Amazon console.(group name : S3FullAccess)
- Go to Security Credentials part.On the right you will see the groups.
- Attach the AmazonS3FullAccess policy to the group.
- If you don't give access you will be facing the following error
2015-07-03 07:28:06,231 WARN [com.amazonaws.services.s3.AmazonS3Client] (default task-1) No content length specified for stream data. Stream contents will be buffered in memory and could result in out of memory errors.
2015-07-03 07:28:10,542 INFO [stdout] (default task-1) Caught an AmazonServiceException, which means your request made it to Amazon S3, but was rejected with an error response for some reason.
2015-07-03 07:28:10,543 INFO [stdout] (default task-1) Error Message: The request signature we calculated does not match the signature you provided. Check your key and signing method. (Service: Amazon S3; Status Code: 403; Error Code: SignatureDoesNotMatch; Request ID: B424D1235CF7B2C4)
2015-07-03 07:28:10,543 INFO [stdout] (default task-1) HTTP Status Code: 403
2015-07-03 07:28:10,544 INFO [stdout] (default task-1) AWS Error Code: SignatureDoesNotMatch
2015-07-03 07:28:10,544 INFO [stdout] (default task-1) Error Type: Client
2015-07-03 07:28:10,545 INFO [stdout] (default task-1) Request ID: B424D1235CF7B2C4
- Define a user and add it to S3FullAccess group.
- Go to recently created user and generate AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY under security credentials part.
- Set the environment variables for AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY
- rhc env-set AWS_ACCESS_KEY_ID='AKIAIXXXXXXHCK2Q' -a maurice
- rhc env-set AWS_SECRET_ACCESS_KEY='ETPvP3JKtXXXXXXXmsn91l+o17QJt' -a maurice
- Add the following dependency for Amazon S3 objects.
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk</artifactId>
<version>1.10.2</version>
</dependency>
- Write a servlet that will accept the posted data and write to S3 as a file.
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.EnvironmentVariableCredentialsProvider;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
/**
* Servlet implementation class S3EventListener
*/
@WebServlet("/S3EventListener")
public class S3EventListener extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public S3EventListener() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Get Method is not supported.");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
AmazonS3 s3client = new AmazonS3Client(new EnvironmentVariableCredentialsProvider());
try {
System.out.println("Uploading a new object to S3 from a post\n");
BufferedReader reader = request.getReader();
ByteArrayInputStream baiStream = new ByteArrayInputStream(reader.readLine().getBytes());
s3client.putObject(new PutObjectRequest(
"sample-bucket", "sample-file", baiStream,new ObjectMetadata() ));
} 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 " +
"an internal error while trying to " +
"communicate with S3, " +
"such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
doGet(request, response);
}
}
http://docs.aws.amazon.com/AWSSdkDocsJava/latest//DeveloperGuide/credentials.html
Hiç yorum yok:
Yorum Gönder