Table creation false in Azure Storage using java - java

I created a java program for create a new table people at the Azure Storage. but i could not able to create a new table.This is my Java code.
CloudStorageAccount storageAccount = CloudStorageAccount.parse(storageConnectionString);
CloudTableClient tableClient = storageAccount.createCloudTableClient();
String tableName = "people";
CloudTable cloudTable = new CloudTable(tableName,tableClient);
//A below line Produce the False.
cloudTable.createIfNotExists();
Can someone Explain this?
Thank u

Related

I have trouble with updating existing aws cloud front CNAMEs

I am trying to add CNAMEs for the existing Distribution in aws cloud front programmatically.
I have tried the following code, but it did not give any result. If someone knows how to do it programmatically. Please kind enough to mention it. Thank you
AmazonCloudFront cloudFront = AmazonCloudFrontAsyncClientBuilder.standard()
.withRegion(Regions.AP_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(route53Manager.getAccessKey(), route53Manager.getSecretKey())))
.build();
GetDistributionConfigResult result = cloudFront.getDistributionConfig(
new GetDistributionConfigRequest("E1EJBNNYJZ6G34"));
Aliases aliases = new Aliases()
.withItems(subDomain)
.withQuantity(1);
DistributionConfig config = result.getDistributionConfig()
.withEnabled(true)
.withAliases(aliases);
It looks like you are missing the update distribution code and a few extra things. See the below code:
AmazonCloudFront cloudFront = AmazonCloudFrontAsyncClientBuilder.standard()
.withRegion(Regions.AP_EAST_1)
.withCredentials(new AWSStaticCredentialsProvider(
new BasicAWSCredentials(route53Manager.getAccessKey(), route53Manager.getSecretKey())))
.build();
//create the request
GetDistributionConfigRequest distributionConfigRequest = new GetDistributionConfigRequest("E1EJBNNYJZ6G34");
//submit the request and get the resulting config
GetDistributionConfigResult distributionConfigResult = cloudFront.getDistributionConfig(distributionConfigRequest);
Aliases aliases = new Aliases()
.withItems(subDomain)
.withQuantity(1);
DistributionConfig config = distributionConfigResult.getDistributionConfig()
.withEnabled(true)
.withAliases(aliases);
//create the update request
UpdateDistributionRequest updateDistributionRequest = new UpdateDistributionRequest(config, distributionConfigRequest.getId(), distributionConfigResult.getETag());
//submit the request to update the config
UpdateDistributionResult updateDistributionResult = cloudfront.updateDistribution(updateDistributionRequest);
//print output of result to console
System.out.println(updateDistributionResult);

Azure Table batch insert from Java Using SAS fails

Trying to use a batch insert to azure table fails if using a SAS ("Shared access signature").
When using account key (which is less secure I guess) it works.
Example code:
StorageCredentialsSharedAccessSignature credentials = new StorageCredentialsSharedAccessSignature("sig=.....");
CloudTableClient cloudTableClient = new CloudTableClient(new URI("https://<storage account>.table.core.windows.net/<tablename>"), credentials);
CloudTable cloudTable = cloudTableClient.getTableReference("<tablename>");
//these 2 will be in a batch
TableServiceEntity d1 = new TableServiceEntity("3333333333333", "22222222222222" + System.currentTimeMillis());
TableServiceEntity d2 = new TableServiceEntity("3333333333333", "eeeeeeeeeee" + System.currentTimeMillis());
//single
TableServiceEntity d3 = new TableServiceEntity("ddddddddddddddddddd", "dddddddddd" + System.currentTimeMillis());
//prepare batch
TableBatchOperation batch = new TableBatchOperation();
batch.insert(d1);
batch.insert(d2);
try {
// this will work (not batch, just to show that regular insert works)
cloudTable.execute(TableOperation.insert(d3));
// this will fail
cloudTable.execute(batch);
} catch (StorageException e) {
//here we get "Unsupported Media Type" (415 error)
e.printStackTrace();
return;
}
System.out.println("OK");
The error I get is:
com.microsoft.azure.storage.StorageException: Unsupported Media Type
at com.microsoft.azure.storage.StorageException.translateException(StorageException.java:89)
at com.microsoft.azure.storage.core.StorageRequest.materializeException(StorageRequest.java:315)
at com.microsoft.azure.storage.core.ExecutionEngine.executeWithRetry(ExecutionEngine.java:175)
at com.microsoft.azure.storage.table.TableBatchOperation.execute(TableBatchOperation.java:418)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:475)
at com.microsoft.azure.storage.table.CloudTable.execute(CloudTable.java:432)
at com.bgprotect.azurestorage.Test.main(Test.java:49)
SAS
sig=<sig>&se=2020-01-01T00%3A00%3A00Z&sv=2015-04-05&tn=<table name>&sp=raud
Based on the issue on Github, please try to change the following line of code:
CloudTableClient cloudTableClient = new CloudTableClient(new URI("https://<storage account>.table.core.windows.net/<tablename>"), credentials);
to:
CloudTableClient cloudTableClient = new CloudTableClient(new URI("https://<storage account>.table.core.windows.net"), credentials);
Essentially don't include the name of the table in the URI. It should only be https://account-name.table.core.windows.net.
P.S. I didn't realize you had also opened an issue on Github regarding this :).

AWS Api Gateway JAVA SDK Add Usage Plan to API Key

The following code will create a new API KEY in AWS API Gateway. Just for fun, I also get an existing usage plan called "Basic" with an id of "1234"
For the life of me I can't find out how to take my newly created API Key and add the existing usage plan to it. This can be done manually on the web portal with the "Add to Usage Plan" button but I want to add my new user to a free plan.
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);
apiGateway = AmazonApiGatewayClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.US_EAST_1).build();
CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
createApiKeyRequest.setName("awesome company);
createApiKeyRequest.setEnabled(true);
createApiKeyRequest.setCustomerId("someid");
CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);
GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
getUsagePlanRequest.setUsagePlanId("1234");
GetUsagePlanResult getUsagePlanResult = apiGateway.getUsagePlan(getUsagePlanRequest);
Any AWS SDK experts know how to connect a usage plan to an api key?
Here's the solution to my post - the key type being "API_KEY" isn't documented anywhere, i found it in some random python sample :/ This creates a new user with an api key and adds them to a usage plan with the api gateway java sdk
BasicAWSCredentials awsCreds = new BasicAWSCredentials(aws_id, aws_key);
apiGateway = AmazonApiGatewayClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(awsCreds))
.withRegion(Regions.US_EAST_1).build();
CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest();
createApiKeyRequest.setName("My awesome new user");
createApiKeyRequest.setEnabled(true);
createApiKeyRequest.setCustomerId(UUID.randomUUID().toString());
CreateApiKeyResult result = apiGateway.createApiKey(createApiKeyRequest);
GetUsagePlanRequest getUsagePlanRequest = new GetUsagePlanRequest();
getUsagePlanRequest.setUsagePlanId(BASIC_USAGE_PLAN_ID);
CreateUsagePlanKeyRequest createUsagePlanKeyRequest = new CreateUsagePlanKeyRequest()
.withUsagePlanId(BASIC_USAGE_PLAN_ID);
createUsagePlanKeyRequest.setKeyId(result.getId());
createUsagePlanKeyRequest.setKeyType("API_KEY");
apiGateway.createUsagePlanKey(createUsagePlanKeyRequest);
This should maybe be a comment instead, but I made an answer for readability (the key type is documented here).
// Client
AmazonApiGateway client = AmazonApiGatewayClientBuilder.standard().withRegion("my region here").build();
// Create new key
CreateApiKeyRequest keyReq = new CreateApiKeyRequest();
keyReq.setName("key name");
keyReq.setDescription("description");
keyReq.setEnabled(true);
CreateApiKeyResult keyRes = client.createApiKey(keyReq);
// Use existing plan
CreateUsagePlanKeyRequest planReq = new CreateUsagePlanKeyRequest();
planReq.setUsagePlanId("my usage plan id");
planReq.setKeyId(keyRes.getId()); // id from new key
planReq.setKeyType("API_KEY");
// add key to plan
client.createUsagePlanKey(planReq);
Note, this example is without a try-catch block

BigQuery - how to register a UDF from the Java Client

I see from the docs that UserDefinedFunctionResource exists, but I can't find the right call to hook it up to a query job. I'd like to add a resource to gs and register it before running a query using the Java API.
Thanks in advance.
You should add UserDefinedFunctionResource items to your JobConfigurationQuery object. Something like this:
Job content = new Job();
JobConfiguration cfg = new JobConfiguration();
JobConfigurationQuery jobConfigurationQuery = new JobConfigurationQuery();
UserDefinedFunctionResource userDefinedFunctionResource = new UserDefinedFunctionResource();
userDefinedFunctionResource.setResourceUri("gs://mybucket/udf.js");
jobConfigurationQuery.setUserDefinedFunctionResources(Lists.newArrayList(
userDefinedFunctionResource
));
cfg.setQuery(jobConfigurationQuery);
content.setConfiguration(cfg);
Bigquery.Jobs.Insert request = bigqueryService.jobs().insert(projectId, content);
Job response = request.execute();

Overriding authentication behavior in jclouds

I'm looking to leverage RackSpace's CloudFiles platform for large object storage (word docs, images, etc). Following some of their guides, I found a useful code snippet, that looks like it should work, but doesn't in my case.
Iterable<Module> modules = ImmutableSet.<Module> of(
new Log4JLoggingModule());
Properties properties = new Properties();
properties.setProperty(LocationConstants.PROPERTY_ZONE, ZONE);
properties.setProperty(LocationConstants.PROPERTY_REGION, "ORD");
CloudFilesClient cloudFilesClient = ContextBuilder.newBuilder(PROVIDER)
.credentials(username, apiKey)
.overrides(properties)
.modules(modules)
.buildApi(CloudFilesClient.class);
The problem is that when this code executes, it tries to log me in the IAD (Virginia) instance of CloudFiles. My organization's goal is to use the ORD (Chicago) instance as primary to be colocated with our cloud and use DFW as a back up environment. The login response results in the IAD instance coming back first, so I'm assuming JClouds is using that. Browsing around, it looks like the ZONE/REGION attributes are ignored for CloudFiles. I was wondering if there is any way to override the code that comes back for authentication to loop through the returned providers and choose which one to login to.
Update:
The accepted answer is mostly good, with some more info available in this snippet:
RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift = cloudFilesClient.unwrap();
CommonSwiftClient client = swift.getApi();
SwiftObject object = client.newSwiftObject();
object.getInfo().setName(FILENAME + SUFFIX);
object.setPayload("This is my payload."); //input stream.
String id = client.putObject(CONTAINER, object);
System.out.println(id);
SwiftObject obj2 = client.getObject(CONTAINER,FILENAME + SUFFIX);
System.out.println(obj2.getPayload());
We are working on the next version of jclouds (1.7.1) that should include multi-region support for Rackspace Cloud Files and OpenStack Swift. In the meantime you might be able to use this code as a workaround.
private void uploadToRackspaceRegion() {
Iterable<Module> modules = ImmutableSet.<Module> of(new Log4JLoggingModule());
String provider = "swift-keystone"; //Region selection is limited to swift-keystone provider
String identity = "username";
String credential = "password";
String endpoint = "https://identity.api.rackspacecloud.com/v2.0/";
String region = "ORD";
Properties overrides = new Properties();
overrides.setProperty(LocationConstants.PROPERTY_REGION, region);
overrides.setProperty(Constants.PROPERTY_API_VERSION, "2");
BlobStoreContext context = ContextBuilder.newBuilder(provider)
.endpoint(endpoint)
.credentials(identity, credential)
.modules(modules)
.overrides(overrides)
.buildView(BlobStoreContext.class);
RestContext<CommonSwiftClient, CommonSwiftAsyncClient> swift = context.unwrap();
CommonSwiftClient client = swift.getApi();
SwiftObject uploadObject = client.newSwiftObject();
uploadObject.getInfo().setName("test.txt");
uploadObject.setPayload("This is my payload."); //input stream.
String eTag = client.putObject("jclouds", uploadObject);
System.out.println("eTag = " + eTag);
SwiftObject downloadObject = client.getObject("jclouds", "test.txt");
System.out.println("downloadObject = " + downloadObject.getPayload());
context.close();
}
Use swift as you would Cloud Files. Keep in mind that if you need to use Cloud Files CDN stuff, the above won't work for that. Also, know that this way of doing things will eventually be deprecated.

Categories

Resources