Google Storage: how to check if an object exists? - google-cloud-storage

What is the best JSON API method to check if an object with the given name exists in a Google storage bucket?

You can use storage.objects.get and check for a 404 response. Alternatively, if you want to create an object only if doesn't already exist, you can set the ifGenerationMatch parameter to 0. If the object already exists, a precondition failure will be returned.

Related

Setting "default" metadata for all+new objects in a GCS bucket?

I run a static website (blog) on Google Cloud Storage.
I need to set a default metadata header for cache-control header for all existing and future objects.
However, editing object metadata instructions show the gsutil setmeta -h "cache-control: ..." command, which doesn't seem to be neither applying to "future" objects in the bucket, nor giving me a way to set a
bucket-wide policy that can be inherited to existing/future objects (since the command is executed per-object).
This is surprising to me because there are features like gsutil defaclwhich let you set a policy for the bucket that is inherited by objects created in the future.
Q: Is there a metadata policy for the entire bucket that would apply to all existing and future objects?
There is no way to set default metadata on GCS objects. You have to set the metadata at write time, or you can update it later (e.g., using gsutil setmeta).
Extracted from this question
According to the documentation, if an object does not have a Cache-Control entry, the default value when serving that object would be public,max-age=3600 if the object is publicly readable.
In the case that you still want to modify this meta-data, you could do that using the JSON API inside a Cloud Funtion that would be triggered every time a new object is created or an existing one is overwritten.

Global Consistency of Deletion in GCS

The following snippet from GCS docs:
Strong global consistency also extends to deletion (DELETE) operations on objects and update (PUT) operations that change existing object and bucket ACLs. If you delete an object and you receive a success response, an immediate attempt to download (GET) the object will result in a 404 Not Found status code. Likewise, if you change ACLs on an object or bucket and you receive a success response, the newly applied object or bucket ACLs are immediately available.
is perplexing, I thought that global consistency means I won't get a 404 Not Found error, right?
You will get a 404 error because the object no longer exists after the delete operation completes successfully. If it weren't for strong consistency, it would be possible to delete an object and then perform a GET on the object and get back the old, pre-deleted state of the object.

What object is returned by using Fetch Request in Core Data?

Apple Documents says "If a context already contains a managed object for an object returned from a fetch, then the existing managed object is returned in the fetch results"
My Question is If I have updatd the object in the context but not saved the context yet then what object will Fetch Request return? Updated object from Context or New one from Datastore.
Your quote answers that question; it will be the one in memory. With that comes the caveat that if you make a new NSManagedObjectContext and perform the fetch request on that context, you will get the object from the data store.
It depends on the includesPendingChanges setting of the fetch request. By default,
includesPendingChanges is YES, which means that the fetch will get currently unsaved
changes.
However, if you use the NSDictionaryResultType result type for the fetch request,
this implicitly implies includesPendingChanges = NO, and you will get only results
from the store.
I guess it returns Updated object from Context.
Note: Due to low reputation i have to put as answer.

Google Cloud Storage : Can we Get file or Search file based on meta data?

While Uploading Object , I have assigned metadata using x-goog-meta-<keyname>.
Currently to get file , we have to use Get Object using Key/Filename.
I want know is it possible like Get Object using META-DATA ?
Is there any way we can directly get/search file by passing metadata ?
Thanks!
No, you cannot.
You can retrieve the metadata via the object name, but you cannot retrieve the object name via the metadata.
If you really needed to, you could create a second bucket that contained objects with the metadata names with data or metadata that referred to the original object name in the first bucket.

How can I change key/name of Amazon S3 object using REST or SOAP?

How can I change key/name of Amazon S3 object using REST or SOAP?
The only way to rename an object is to copy the old object to a new object, and set the new name on the new copy.
The REST call you need is detailed here.
Syntax
PUT /destinationObject HTTP/1.1
Host: destinationBucket.s3.amazonaws.com
x-amz-copy-source: /source_bucket/sourceObject
x-amz-metadata-directive: metadata_directive
x-amz-copy-source-if-match: etag
x-amz-copy-source-if-none-match: etag
x-amz-copy-source-if-unmodified-since: time_stamp
x-amz-copy-source-if-modified-since: time_stamp
<request metadata>
Authorization: signatureValue
Date: date
This implementation of the PUT operation creates a copy of an object
that is already stored in Amazon S3. A PUT copy operation is the same
as performing a GET and then a PUT. Adding the request header,
x-amz-copy-source, makes the PUT operation copy the source object into
the destination bucket.
Keep in mind the existing ACLs, however:
When copying an object, you can preserve most of the metadata
(default) or specify new metadata. However, the ACL is not preserved
and is set to private for the user making the request.