Skip to content

Instantly share code, notes, and snippets.

@leonmax
Last active November 14, 2022 04:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leonmax/c464fc6a12f62b90052068f23b93c9f9 to your computer and use it in GitHub Desktop.
Save leonmax/c464fc6a12f62b90052068f23b93c9f9 to your computer and use it in GitHub Desktop.
sample to load from oss auth_code and download files recurisvely
import base64
import json
import pathlib
import re
import sys
from urllib.error import URLError
import oss2
OSS_URL_PATTERN = re.compile(
r"oss://{bucket}/{path}".format(
bucket=r"(?P<bucket>[^/]+)",
path=r"(?P<path>.+)",
)
)
def _parse_url(url):
m = OSS_URL_PATTERN.match(url)
if not m:
raise URLError(f"URL {url} is not parsable")
bucket_name = m.group("bucket")
remote_path = m.group("path")
return bucket_name, remote_path
def main():
decoded = base64.b64decode(sys.stdin.read())
token = json.loads(decoded)
auth = oss2.StsAuth(token['id'], token['secret'], token['stoken'])
bucket_name, remote_path = _parse_url(token['osspath'])
bucket = oss2.Bucket(auth, token['eptpl'], bucket_name)
for s in bucket.list_objects_v2(prefix=remote_path).object_list:
if s.key.endswith('/'):
continue
print()
print(f"oss://{bucket_name}/{s.key}")
print(f"size: {s.size}")
# Download
p = pathlib.Path(s.key).relative_to(remote_path)
p.parent.mkdir(parents=True, exist_ok=True)
with p.open("wb") as f:
f.write(bucket.get_object(s.key).read())
if __name__ == '__main__':
main()
pip install oss2
echo [YOUR_OSS_AUTH_CODE] | python oss_auth_code.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment