Java tutorial
/** * @file * @brief AmazonClientManager * @author tmor * @licence Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 * @note * - ?static??????? * * $Revision:$ */ package jp.aws.test; import jp.aws.test.ec2.EC2Region; import com.amazonaws.ClientConfiguration; import com.amazonaws.auth.AWSCredentials; import com.amazonaws.auth.BasicAWSCredentials; import com.amazonaws.services.s3.AmazonS3Client; import com.amazonaws.services.ec2.AmazonEC2Client; import android.content.Context; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.util.Log; public class AmazonClientManager { private static final String LOG_TAG = "AmazonClientManager"; private Context context = null; private AmazonEC2Client ec2Client = null; private AmazonS3Client s3Client = null; private int connectionTimeout = 15 * 1000; // (ms) public AmazonClientManager(Context context) { this.context = context; } public AmazonEC2Client ec2() { validateCredentials(); return ec2Client; } public AmazonS3Client s3() { validateCredentials(); return s3Client; } /** * ??? * * @return */ public boolean hasCredentials() { // ??? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); if (prefs.getString("prefs_account_access_key", "") == "" && prefs.getString("prefs_account_secret_key", "") == "") { return false; } return true; } /** * ? */ public void validateCredentials() { if (s3Client == null || ec2Client == null) { Log.i(LOG_TAG, "Creating New Clients."); // ??? SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String access_key = prefs.getString("prefs_account_access_key", ""); String secret_key = prefs.getString("prefs_account_secret_key", ""); // ? ClientConfiguration clientconfiguration = new ClientConfiguration(); clientconfiguration.setConnectionTimeout(this.connectionTimeout); // (ms) AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key); s3Client = new AmazonS3Client(credentials, clientconfiguration); ec2Client = new AmazonEC2Client(credentials, clientconfiguration); // this.changeRegion(); } } /** * ? */ public void clearClients() { s3Client = null; ec2Client = null; } /** * */ public void changeRegion() { validateCredentials(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String region = prefs.getString("region", EC2Region.DEFAULT_REGION); ec2Client.setEndpoint("ec2." + region + ".amazonaws.com"); // } }