Created
November 10, 2023 10:11
-
-
Save leplatrem/98b0ff1818053232082b8551b26c4a0d to your computer and use it in GitHub Desktop.
Upload file to Remote Settings in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
import sys | |
import uuid | |
import mimetypes | |
import requests | |
filepath = sys.argv[1] | |
SERVER_URL = "https://remote-settings-dev.allizom.org/v1" | |
COLLECTION_ID = os.getenv("COLLECTION_ID", "product-integrity") | |
AUTHZ = os.getenv("AUTHZ", "") | |
record_id = uuid.uuid4() | |
attachment_uri = f"{SERVER_URL}/buckets/main-workspace/collections/{COLLECTION_ID}/records/{record_id}/attachment" | |
filename = os.path.basename(filepath) | |
filecontent = open(filepath, "rb").read() | |
mimetype, _ = mimetypes.guess_type(filepath) | |
multipart = [("attachment", (filename, filecontent, mimetype))] | |
data = {"field": "foo"} | |
permissions = {"write": ["account:alice"]} | |
resp = requests.post( | |
attachment_uri, | |
data={ | |
"data": json.dumps(data), | |
"permissions": json.dumps(permissions), | |
}, | |
files=multipart, | |
headers={"Authorization": AUTHZ} | |
) | |
resp.raise_for_status() | |
json.dump(resp.json(), sys.stdout, indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment