How to create s3 Key object without validation? - boto

Is there a way to create a Key for a connection without validation using boto? The docs say there is a validate parameter, but it doesn't exist in the 2.23 source (which is supposedly the same version as the docs).
I need a workaround to avoid doing the lookup on the key.

The get_key() method in boto.s3.bucket.Bucket performs a HEAD request on the object to verify that it exists. If you are sure the object exists and don't want the overhead of the HEAD request, simply create the Key object directly like this:
import boto.s3
from boto.s3.key import Key
conn = boto.s3.connect_to_region('us-east-1')
bucket = conn.get_bucket('mybucket', validate=False)
key = Key(bucket, 'mykeyname')
This avoids the HEAD request and still allows you to perform normal operations on the Key object. Note, however, that the HEAD request retrieves certain metadata about the Key in question such as its content-type, size, ETag, etc. The Key object constructed directly will not have that information available.

Related

How to save JSON to table storage in the azure without deserialization?

I have a message coming to me from the outside world in JSON format. I know for sure what is going to be partition key and row key in the JSON but I don't want to deserialize all properties of that object. How can I save this string using Table API SDK available?
E.g. that is my input string:
{
"name":"John Doe",
"age":38,
"country":"USA",
"phone_number":"+123456789",
"current_balance":100500,
"subscribed":false
}
For that particular example, I would like to define the name as a row key and country as a partition key. In the same time, I don't want to perform deserialization of the whole entity but store all the data available. However, using Microsoft.Azure.Cosmos.Table package I found no way to just dump this data directly to the table. What is a preferable option to do that?

Handling duplicate key values in swift dictionary

I have to comply with a Restful request call using Swift 3 that unfortunately contains duplicate keys as shown below. I originally placed the data in a dictionary to pass into a JSON serialization call.
[
"mid":"3",
"attributes":[
"carID":["a":54123,"b":424,"c":"13242"],
"update":"true"
"update":"gold"
]
]
Notice that update occurs twice, which causes this error:
fatal error: Dictionary literal contains duplicate keys
I've reviewed this question but doesn't seem to have a pertinent answer.
I thought about using an array but I think it would lose the key value pair relationship that I need for the JSON serialization.
I've seen some articles on using structs but I'm not sure that would address the duplicate key issue
Any recommendations? I'm unable to change the parameter names in the request as it is a third party product.
BTW, I don't think my question is a duplicate of How to avoid duplicate key error in swift when iterating over a dictionary – Ssswift. That example should truly be a struct/class since it is one key value pair (age:age value) that should be in a class or struct. My example has a repeating key with each of the two instances of the repeating key having different meanings (should they update, and what the updated member type should be). I don't see how it would be applicable but am open to enlightenment on it.
Thanks

Check if value exists in Lua table

I am running Lua on ESP8266 Wifi module with NodeMCU firmware. My application is listening on TCP port for JSON requests. When I get the request I parse it using:
jsonRequest = json.decode(request)
So then I can access desired value with:
jsonRequest.object.state
Everything works perfectly until I send an invalid JSON (without "object"). When that happens I get this error: Lua API (attempt to index a nil value) and my program stops with execution.
MY PROBLEM: I would like to check if my table contains that key before accessing, but I can't find a way to do it.
I could do it with pairs function and loop through all keys and check if there is the right one, but that would require lots of code because I have multiple nested objects in my JSON.
Any ideas?
To check if the table jsonRequest contains the key "object", use:
if jsonRequest.object ~= nil then
If the values stored in the table won't be the boolean value false, you can also use:
if jsonRequest.object then

Edit json object by lua script in redis

I want edit my json object before back from the Redis server,
In my Redis server I have 4 keys:
user:1 {"Id":"1","Name":"Gholzam","Posts":"[]"}
user:1:post:1 {"PostId":"1","Content":"Test content"}
user:1:post:2 {"PostId":"2","Content":"Test content"}
user:1:post:3 {"PostId":"3","Content":"Test content"}
I want to get this context by lua script,How ? :
{"Id":"1","Name":"Gholzam","Posts":"[{"PostId":"1","Content":"Test
content"},{"PostId":"1","Content":"Test
content"},{"PostId":"1","Content":"Test content"}]}
The choice of client here is largely irrelevant; the important thing to do is: figure out the data storage. You say you have 4 keys - but it is not obvious to me how we we know, given user:1, what the posts are. Common approaches there include:
have a set called user:1:posts (or something similar) which contains either the full keys (user:1:post:1, etc) or the relative keys (1, etc)
have a hash called user:1:posts (or something similar) which contains the posts keyed by their id
I'd be tempted to use the latter approach, as it is more direct - so I might have:
user:1, a string with contents {"Id":"1","Name":"Gholzam","Posts":"[]"}
user:1:posts, a hash with 3 pairs:
key 1 with value {"PostId":"1","Content":"Test content"}
key 2 with value {"PostId":"2","Content":"Test content"}
key 3 with value {"PostId":"3","Content":"Test content"}
Then you can use hgetall or hvals to get the posts easily.
The second part is how to manipulate json at the server. The good news here is that redis provides access to json tools inside lua via cjson.
I am an expert in neither cjson nor lua; however, frankly my advice is: don't do this. IMO, redis works best if you let it focus on what it is great at: storage and retrieval. You probably can bend it to your whim, but I would be very tempted to do any json manipulation outside of redis.

Flex 4.5 JSON check for existence of key before exception?

Is it possible to check for the existence of a key before Flex 4.5 JSON throws an exception for key not found? I have some json data where some keys are not always present.
I am using CallResponder...
The issue is that when you try accessing callResponder.lastResponse.key - and, say, key is not always present in your json, Flex won't be able to parse it. This happens even if you check if (callResponder.lastResponse.key) - the error occurs on the if line, in that case
(See old error dump here)
The answer provided below works for cases where the keys are top level nodes in the JSON. However, its seems unable to parse existence for child keys. I had not expected there to be a difference between parsing for top level nodes and child nodes, but apparently that is the case.
For clarity's sake (as I did not mention I am trying to parse for child keys in my original question), I've created a new question here that specifically asks how to parse for child keys, using the method below -- or another method, if need be: Flex 4.5 How do you check for JSON child node key existence (using hasOwnProperty or other methods)
How do you process your JSON data?
In my memory, I use JSON.decode(String), and got an array. For each object, you can use obj.hasOwnProperty(key) to check whether it's present.