I am trying to upload content to a Google bucket using Python SDK in a forked process on a Linux host (Ubuntu 18.04) The operation fails with status code 403:
google.api_core.exceptions.Forbidden: 403 POST https://www.googleapis.com/upload/storage/v1/b/temp-compare/o?uploadType=multipart: ('Request failed with status code', 403, 'Expected one of', )
Example code:
import random
import string
import json
import os
import requests
from datetime import datetime, timedelta
from google.cloud import storage
def upload_to_bucket(plainText):
gcp_client = storage.Client.from_service_account_json("/path/to/google-bucket-credentials.json")
bucket = gcp_client.get_bucket('bucket_name')
bucket_file_name = random_file_name('json', 10)
blob = bucket.blob(bucket_file_name)
blob.upload_from_string(plainText)
url = blob.generate_signed_url(expiration=datetime.utcnow() + timedelta(hours=48), method="GET")
return url
def random_file_name(ext, length):
return "%s.%s" % (''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(length)), ext)
if __name__ == "__main__":
os.fork()
url = upload_to_bucket("just for test")
res = requests.get(url)
print(res.content.decode('utf8'))
I have tried using the solutions offered in google-api-python-client broken because of OAuth2? with no lock.
How can this be solved?