Skip to content

Commit

Permalink
Merge pull request #263 from nationalarchives/fix/correct-uri-formats…
Browse files Browse the repository at this point in the history
…-for-s3

(Fix) When moving & retreiving items in S3, strip the leading slash from keys
  • Loading branch information
dragon-dxw authored Jul 6, 2022
2 parents 170394e + 1914eb6 commit 2cb2201
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 5 additions & 1 deletion judgments/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ def set_metadata(old_uri, new_uri):
def copy_assets(old_uri, new_uri):
client = create_s3_client()
bucket = env("PRIVATE_ASSET_BUCKET")
old_uri = old_uri.lstrip("/")
new_uri = new_uri.lstrip("/")

response = client.list_objects(Bucket=bucket, Prefix=old_uri)

for result in response.get("Contents", []):
old_key = str(result["Key"])
new_key = build_new_key(old_key, new_uri.lstrip("/"))
new_key = build_new_key(old_key, new_uri)
if new_key is not None:
try:
source = {"Bucket": bucket, "Key": old_key}
Expand Down
2 changes: 2 additions & 0 deletions judgments/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ def generate_docx_url(uri: str):
return ""

client = create_s3_client()
uri = uri.lstrip("/")

key = f'{uri}/{uri.replace("/", "_")}.docx'

Expand All @@ -439,6 +440,7 @@ def generate_pdf_url(uri: str):
return ""

client = create_s3_client()
uri = uri.lstrip("/")

key = f'{uri}/{uri.replace("/", "_")}.pdf'

Expand Down

0 comments on commit 2cb2201

Please sign in to comment.