Answers: <p>It may be obvious from the example, but I did not get it. I thought that &quot;metadata&quot; would be all of your metadata and that you could overwrite it as you like, so that <code>{'color': 'Red', 'name': 'Test'}</code> would actually replace the whole metadata at hand.</p>
  <p>I even created</p>
  <pre class="lang-py prettyprint-override"><code>    blob.metadata = {
          **metadata,
          **{
              &quot;rowcount&quot;: 99,
              &quot;columncount&quot;: 55
          },
      }
  </code></pre>
  <p>to join the filled metadata with the additional attributes. But this is of course not needed. And you also cannot overwrite any default attributes of the metadata either.</p>
  <p>If you want to save something in the metadata, you <em>must</em> add them as dictionary to the &quot;metadata&quot; attribute.</p>
  <p>In my case, when I want to store the rowcount, I just need:</p>
  <pre class="lang-py prettyprint-override"><code>def set_blob_metadata(bucket_name, blob_name):
      &quot;&quot;&quot;Set a blob's metadata.&quot;&quot;&quot;
      # bucket_name = 'your-bucket-name'
      # blob_name = 'your-object-name'
  
      storage_client = storage.Client()
      bucket = storage_client.bucket(bucket_name)
      blob = bucket.get_blob(blob_name)
      metadata = {'rowcount': 555}
      blob.metadata = metadata
      blob.patch()
  
      print(&quot;The metadata for the blob {} is {}&quot;.format(blob.name, blob.metadata))
  </code></pre>
  <h4>Wrap up</h4>
  <p>I thought at first that the <code>metadata</code> attribute is just all metadata in one attribute as a dictionary, but instead, it is None by default and you can fill it with a dictionary of your choice to add new attributes. Only this &quot;metadata&quot; attribute can be filled at all, any other attribute throws <code>AttributeError: can't set attribute&quot;</code>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2022-01-25T18:40:00.580Z
Answers: 2022-01-25T18:40:00.580Z
Answers: questionto42
Body: <p>I try to add the rowcount to the metadata of the Google Cloud Storage file from inside the Python Google Cloud Function.</p>
  <p>I follow the official Cloud Storage Docs example of <a href="https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-python" rel="nofollow noreferrer">&quot;Edit object metadata&quot;</a> by just trying to change an attribute the same way as in the code:</p>
  <pre class="lang-py prettyprint-override"><code>from google.cloud import storage
  
  
  def set_blob_metadata(bucket_name, blob_name):
      &quot;&quot;&quot;Set a blob's metadata.&quot;&quot;&quot;
      # bucket_name = 'your-bucket-name'
      # blob_name = 'your-object-name'
  
      storage_client = storage.Client()
      bucket = storage_client.bucket(bucket_name)
      blob = bucket.get_blob(blob_name)
      metadata = {'color': 'Red', 'name': 'Test'}
      blob.metadata = metadata
      blob.patch()
  
      print(&quot;The metadata for the blob {} is {}&quot;.format(blob.name, blob.metadata))
  </code></pre>
  <p>My code, just setting one attribute:</p>
  <pre class="lang-py prettyprint-override"><code>from os import environ
  from google.cloud import storage
  
      ...
      storage_client = storage.Client()
      bucket = storage_client.bucket(environ[&quot;BUCKET_NAME&quot;])
      # Retrieve a blob, and its metadata, from Google Cloud Storage.
      # Note that `get_blob` differs from `Bucket.blob`, which does not
      # make an HTTP request.
      blob = bucket.get_blob(environ[&quot;FILE_NAME_TEST&quot;])   
      print(&quot;Size: {} bytes&quot;.format(blob.size))
      print(&quot;Custom Time: {}&quot;.format(blob.custom_time))
      blob.size = 99
  </code></pre>
  <p>But this throws an error in the logs:</p>
  <pre><code>blob.size = 99 AttributeError: can't set attribute&quot;
  </code></pre>
  <p>How to write to an attribute using the Python google_cloud_storage package?</p>
  
Tags: <google-cloud-platform><google-cloud-storage>
Title: How do I set metadata of a blob from inside a Python Google Cloud Function? Test fails with "blob.size = 99 AttributeError: can't set attribute"
UserDisplayName: questionto42
ThreadText: # How do I set metadata of a blob from inside a Python Google Cloud Function? Test fails with "blob.size = 99 AttributeError: can't set attribute"
  
  (Asked by: username_0 on 2022-01-25)
  
  I try to add the rowcount to the metadata of the Google Cloud Storage file from inside the Python Google Cloud Function.
  
  I follow the official Cloud Storage Docs example of ["Edit object metadata"](https://cloud.google.com/storage/docs/viewing-editing-metadata#storage-view-object-metadata-python) by just trying to change an attribute the same way as in the code:
  
  ```py
  from google.cloud import storage
  
  def set_blob_metadata(bucket_name, blob_name):
      """Set a blob's metadata."""
      # bucket_name = 'your-bucket-name'
      # blob_name = 'your-object-name'
  
      storage_client = storage.Client()
      bucket = storage_client.bucket(bucket_name)
      blob = bucket.get_blob(blob_name)
      metadata = {'color': 'Red', 'name': 'Test'}
      blob.metadata = metadata
      blob.patch()
  
      print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))
  
  ```
  
  My code, just setting one attribute:
  
  ```py
  from os import environ
  from google.cloud import storage
  
      ...
      storage_client = storage.Client()
      bucket = storage_client.bucket(environ["BUCKET_NAME"])
      # Retrieve a blob, and its metadata, from Google Cloud Storage.
      # Note that `get_blob` differs from `Bucket.blob`, which does not
      # make an HTTP request.
      blob = bucket.get_blob(environ["FILE_NAME_TEST"])   
      print("Size: {} bytes".format(blob.size))
      print("Custom Time: {}".format(blob.custom_time))
      blob.size = 99
  
  ```
  
  But this throws an error in the logs:
  
  ```
  blob.size = 99 AttributeError: can't set attribute"
  
  ```
  
  How to write to an attribute using the Python google\_cloud\_storage package?
  
  **Tags:** google-cloud-platform, google-cloud-storage
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-01-25. Score: 0 | Accepted answer
  
  It may be obvious from the example, but I did not get it. I thought that "metadata" would be all of your metadata and that you could overwrite it as you like, so that `{'color': 'Red', 'name': 'Test'}` would actually replace the whole metadata at hand.
  
  I even created
  
  ```py
      blob.metadata = {
          **metadata,
          **{
              "rowcount": 99,
              "columncount": 55
          },
      }
  
  ```
  
  to join the filled metadata with the additional attributes. But this is of course not needed. And you also cannot overwrite any default attributes of the metadata either.
  
  If you want to save something in the metadata, you *must* add them as dictionary to the "metadata" attribute.
  
  In my case, when I want to store the rowcount, I just need:
  
  ```py
  def set_blob_metadata(bucket_name, blob_name):
      """Set a blob's metadata."""
      # bucket_name = 'your-bucket-name'
      # blob_name = 'your-object-name'
  
      storage_client = storage.Client()
      bucket = storage_client.bucket(bucket_name)
      blob = bucket.get_blob(blob_name)
      metadata = {'rowcount': 555}
      blob.metadata = metadata
      blob.patch()
  
      print("The metadata for the blob {} is {}".format(blob.name, blob.metadata))
  
  ```
  
  #### Wrap up
  
  I thought at first that the `metadata` attribute is just all metadata in one attribute as a dictionary, but instead, it is None by default and you can fill it with a dictionary of your choice to add new attributes. Only this "metadata" attribute can be filled at all, any other attribute throws `AttributeError: can't set attribute"`.
  
  
  ---
  
  