How to list all object's versions from a bucket and their respective metadata (x-amz-meta-version) - amazon-s3

to achieve what's in the title I am trying to do a couple steps (using python & AWS SDK), which I will list after I mention the error I am getting is 412 "At least one of the preconditions you specified did not hold" in the second iteration of the method get_object, when I send it the parameters: bucket, key and IfMatch (it fails in this line).
List all object's versions with the following code
s3 = boto3.client('s3')
response = s3.list_object_versions(
                    Bucket='my-bucket',
                    Prefix='file.exe'
                )
obj_versions = response["Versions"]
This totally works, but I need the versions I set in metadata (x-amz-meta-version), to get each object's metadata version I am trying to do the following:
obj_info = []
for obj_version in obj_versions:
    obj = s3.get_object(
            Bucket='my-bucket',
            Key='file.exe',
            IfMatch=obj_version['ETag']
        )
    obj_info.append(obj['Metadata']['version'])
And that's it, at the moment it only works until the second iteration oddly enough, it always fails with a 412 "At least one of the preconditions you specified did not hold" in the s3.get_object (IfMatch) line. I know for sure the error is in the precondition IfMatch, but I have no idea what's wrong... I have printed every ETag it's receiving and they're all valid, it should be able to get the object.
​
Thank you for reading my post.

s3.get_object() tries retrieve the current version of the object unless you include VersionId of another version, so the behavior you are observing is correct -- the ETag of the current version does not match the etag value that you're passing, and thus the precondition fails. ETag isn't a lookup key, and IfMatch isn't a selector (it's a conditional request -- "don't give me the object unless the precondition matches") and in any event, multiple versions of an object can have the same ETag if the object versions have identical payload (depending on the type of encryption you are using on the bucket -- the standard only requires etags to differ if payload differs; it does not technically require them to match if the payload matches).
Note also that if you only want the metadata, then for both cost and performance reasons you should use s3.head_object() to avoid fetching the object payload.

Related

attributes.headers getting lost after a http Request call in Mulesoft?

I am getting some attributes in an API but all getting lost after an HTTP request connector in mule4.
why is it happening?
Look in the connector's configuration properties -> advanced tab for the connector configuration (in this case the HTTP connector's "request" operation) and you'll find a target variable and target value. If you fill in the target with a name - this does an enrichment to avoid overwriting the Mule message. If you leave it blank (the default) it will save the message (attributes, payload) over the top of the existing one - which is what you're seeing now. This mirrors the old mule 3 functionality, but sometimes you want it to leave what you have there alone.
So for the target value you get to pick exactly what gets saved.. If you want just payload: put that in. If you want both payload and attributes - I'd use "message" as that will mean you get both payload and attributes saved in the variable. Of course you may not want as much saved, so feel free to put in whatever dataweave expression you like - so you could even create something with bits from anywhere like:
{
statusCode: attributes.statusCode,
headers: attributes.headers,
payload: payload
}
A connector operation may replace the attributes with those of the operation. If you need to preserve the previous attributes you need to save them to a variable.
This is a default behaviour of MuleSoft. Whenever request crosses to transport barrier it losses existing attributes. You need to preserve attribute before HTTP Request.

Counting the number of response codes in JMeter 4.0

I run some load tests (all endpoints) and we do have a known issue in our code: if multiple POST requests are sent in the same time we do get a duplicate error based on a timestamp field in our database.
All I want to do is to count timeouts (based on the message received "Service is not available. Request timeout") in a variable and accept this as a normal behavior (don't fail the tests).
For now I've added a Response Assertion for this (in order to keep the tests running) but I cannot tell if or how many timeout actually happen.
How can I count this?
Thank you
I would recommend doing this as follows:
Add JSR223 Listener to your Test Plan
Put the following code into "Script" area:
if (prev.getResponseDataAsString().contains('Service is not available. Request timeout')) {
prev.setSampleLabel('False negative')
}
That's it, if sampler will contain Service is not available. Request timeout in the response body - JMeter will change its title to False negative.
You can even mark it as passed by adding prev.setSuccessful(false) line to your script. See Apache Groovy - Why and How You Should Use It article fore more information on what else you can do with Groovy in JMeter tests
If you just need to find out the count based on the response message then you can save the performance results in a csv file using simple data writer (configure for csv only) and then filter csv based on the response message to get the required count. Or you can use Display only "errors" option to get all the errors and then filter out based on the expected error message.
If you need to find out at the runtime then you can use aggregate report listener and use "Errors" checkbox to get the count of failure but this will include other failures also.
But, if you need to get the count at the run time to use it later then it is a different case. I am assuming that it is not the case.
Thanks,

Can PUT request change the URL?

My application with a HTTP API provides some resources that are identified by name. So, the URLs are constructed as the following:
/path/to/resources/<name>
For example:
/path/to/resources/my_resource
The resources can be updated with PUT operations. It is also allowed to rename a resource with such an update.
PUT /path/to/resources/my_resource
{"name": "new_name", <other properties>}
Response:
HTTP/1.1 204 No content
As a result, the updated resource is now accessible under a new URL:
GET /path/to/resources/new_name
Response:
HTTP/1.1 200 OK
{"name": "new_name", <other properties>}
The old URL is no longer valid:
GET /path/to/resources/my_resource
Response:
HTTP/1.1 404 Not found
Is such behavior correct? Should the PUT operation return the Location header with a new URL? Is it OK to return the Location header with the 204 No content status?
After writing this question I found another, quite similar: REST API Design : Is it ok to change the resource identifier during a PUT call?
The accepted answer was that it is allowed, but not recommended. Still don't know what about the Location header, though.
By changing a resource identifier, I understand you are deleting a resource and creating a new one. So, the approach you described in the question is basically a delete operation using the wrong HTTP verb.
According to the RFC 7231, the current reference for HTTP/1.1, PUT requests are used to create or replace a resource:
4.3.4. PUT
The PUT method requests that the state of the target resource be
created or replaced with the state defined by the representation
enclosed in the request message payload.
[...]
If the target resource does not have a current representation and the
PUT successfully creates one, then the origin server MUST inform the
user agent by sending a 201 (Created) response. If the target
resource does have a current representation and that representation
is successfully modified in accordance with the state of the enclosed
representation, then the origin server MUST send either a 200 (OK) or
a 204 (No Content) response to indicate successful completion of the
request.
[...]
I would do the following when the resource identifier needs to be changed:
Perform a DELETE in the existing resource. The response would be a 204 indicating the request was fulfilled.
Perform a POST or PUT to create the resource using the new identifier. The response would be a 201 indicating the resource was created. The response would contain a Location header indicating where the resource is located.
To replace the state of the target resource (keeping the resource identifier), perform a PUT and return a 204 to indicate the operation succeeded.
I don't know about your requirements, but I wouldn't allow the user to change or create the identifier of a resource. I would assume the resource identifier is immutable and should be generated by the application (UUID, of example) or an identifier generated by the database.
I agree with Cássio Mazzochi Molin's answer. However the question is theoretical one, whether renaming the resource really changes the 'identity' of a resource.
For example if a person's name changes, that does not change who the person is. I still would like the URI I previously got for the "same" person to work, even after the name change.
So I guess my point is not to include non-identity related information into the URI. Include an Id number or similar content-unrelated information.
Don't do a DELETE and PUT to another URI (don't relocate the resource) if the identity of the object did not change.

RESTful API - Ignore invalid PUT request?

I build a simple API for a bookmark manager, where the URL of record should only be stored once. Record 001 with www.example.com already exists beside record 002 with www.stuff.com.
If I update record 002 with the url www.example.com, should I ignore the complete request and send back an error message or is it better to update all valid parts and just send an errror message, that says, that the url/bookmark already exists?
With a PUT, the expectation is that the entire operation will succeed or fail:
The PUT method requests that the state of the target resource be
created or replaced with the state defined by the representation
enclosed in the request message payload. A successful PUT of a given
representation would suggest that a subsequent GET on that same
target resource will result in an equivalent representation being
sent in a 200 (OK) response.
https://www.rfc-editor.org/rfc/rfc7231#section-4.3.4
You should send an error for an invalid PUT (since the URL already exists and cannot be in two records at the same time) and not apply any of the other updates.
For partial updates you might consider a PATCH, but in this case I don't think you would go this direction since:
If the entire patch document
cannot be successfully applied, then the server MUST NOT apply any of
the changes.
https://www.rfc-editor.org/rfc/rfc5789

getContentServiceInfo1 returns truncated data regardless of reqSpecifier

Calling the getContentServiceInfo1 REST API seems to return the same data regardless of provided reqSpecifier and notrim values. I am using this call as part of the refresh account flow to determine whether the service requires MFA. Is there another call that I can make to achieve the same thing?
I noticed that the values accepted for reqSpecifier are essentially binary bit masks but providing the flag as 10000000, or even 0b10000000 for that matter, instead of 128 does not change the results. What value and value type should I be providing for reqSpecifier to get more than the basic level information? Relevant request and return information when using this API call for E*Trade is below.
Request (cobrand token is omitted):
{"cobSessionToken":"",
"contentServiceId":24,
"reqSpecifier":128,
"notrim":true}
Response (this is always the response I get unless I provide arguments that result in an error):
{"contentServiceId":24,
"siteId":744,
"containerInfo":{"containerName":"stocks","assetType":1}}
This response comes when you pass "notrim" value as "false", if you are passing it "false" then please pass it as true. It should work.