{"skill":{"slug":"aliyun-oss-2","displayName":"Pywayne Aliyun Oss","summary":"Manage Aliyun OSS buckets in Python with upload, download, list, read, delete, copy, and move operations supporting authenticated and anonymous access.","description":"---\nname: pywayne-aliyun-oss\ndescription: Aliyun OSS (Object Storage Service) file management toolkit for Python. Use when working with Aliyun OSS to upload files, download files, list objects, delete files, manage directories, read file contents, check file existence, get file metadata, copy and move objects. Supports both authenticated (with API key/secret for write operations) and anonymous access (read-only).\n---\n\n# Pywayne Aliyun OSS\n\n`pywayne.aliyun_oss.OssManager` provides a comprehensive toolkit for managing Aliyun OSS (Object Storage Service) buckets.\n\n## Quick Start\n\n```python\nfrom pywayne.aliyun_oss import OssManager\n\n# Initialize with write permissions\noss = OssManager(\n    endpoint=\"https://oss-cn-xxx.aliyuncs.com\",\n    bucket_name=\"my-bucket\",\n    api_key=\"your_api_key\",\n    api_secret=\"your_api_secret\"\n)\n\n# Initialize with read-only (anonymous) access\noss = OssManager(\n    endpoint=\"https://oss-cn-xxx.aliyuncs.com\",\n    bucket_name=\"my-bucket\",\n    verbose=False  # Disable verbose output\n)\n```\n\n## Upload Operations\n\n### Upload a local file\n\n```python\noss.upload_file(key=\"data/sample.txt\", file_path=\"./sample.txt\")\n```\n\n### Upload text content\n\n```python\noss.upload_text(key=\"config/settings.json\", text='{\"key\": \"value\"}')\n```\n\n### Upload an image (numpy array)\n\n```python\nimport cv2\nimage = cv2.imread(\"photo.jpg\")\noss.upload_image(key=\"photos/photo.jpg\", image=image)\n```\n\n### Upload entire directory\n\n```python\noss.upload_directory(local_path=\"./local_folder\", prefix=\"remote_folder/\")\n```\n\n## Download Operations\n\n### Download a single file\n\n```python\n# Preserve directory structure: downloads/data/sample.txt\noss.download_file(key=\"data/sample.txt\", root_dir=\"./downloads\")\n\n# Use only basename: downloads/sample.txt\noss.download_file(key=\"data/sample.txt\", root_dir=\"./downloads\", use_basename=True)\n```\n\n### Download files with prefix\n\n```python\noss.download_files_with_prefix(prefix=\"photos/\", root_dir=\"./downloads\")\n```\n\n### Download entire directory\n\n```python\noss.download_directory(prefix=\"photos/\", local_path=\"./downloads\")\n```\n\n## List Operations\n\n### List all keys in bucket\n\n```python\nkeys = oss.list_all_keys()  # Returns sorted list\n```\n\n### List keys with prefix\n\n```python\nkeys = oss.list_keys_with_prefix(prefix=\"data/\")\n```\n\n### List directory contents (first level only)\n\n```python\ncontents = oss.list_directory_contents(prefix=\"data/\")\n# Returns: [(\"file1.txt\", False), (\"subdir\", True), ...]\n```\n\n## Read Operations\n\n### Read file content as string\n\n```python\ncontent = oss.read_file_content(key=\"config/settings.json\")\n```\n\n### Check if file exists\n\n```python\nif oss.key_exists(\"data/sample.txt\"):\n    print(\"File exists\")\n```\n\n### Get file metadata\n\n```python\nmetadata = oss.get_file_metadata(\"data/sample.txt\")\n# Returns: {'content_length': 1234, 'last_modified': ..., 'etag': ..., 'content_type': ...}\n```\n\n## Delete Operations\n\n### Delete a single file\n\n```python\noss.delete_file(key=\"data/sample.txt\")\n```\n\n### Delete files with prefix\n\n```python\noss.delete_files_with_prefix(prefix=\"temp/\")\n```\n\n## Copy and Move Operations\n\n### Copy object within bucket\n\n```python\noss.copy_object(source_key=\"data/original.txt\", target_key=\"backup/original.txt\")\n```\n\n### Move object within bucket\n\n```python\noss.move_object(source_key=\"data/temp.txt\", target_key=\"archive/temp.txt\")\n```\n\n## Important Notes\n\n- **Write permissions**: Upload, delete, copy, and move operations require `api_key` and `api_secret`\n- **Anonymous access**: Omit `api_key` and `api_secret` for read-only access\n- **Directory handling**: OSS doesn't have real directories - use prefixes (keys ending with `/`)\n- **Natural sorting**: `list_all_keys()` and `list_keys_with_prefix()` use natural sorting by default\n- **Verbose output**: All methods print status messages when `verbose=True` (default)\n","tags":{"latest":"0.1.0"},"stats":{"comments":0,"downloads":870,"installsAllTime":0,"installsCurrent":0,"stars":0,"versions":1},"createdAt":1771106042484,"updatedAt":1778491542212},"latestVersion":{"version":"0.1.0","createdAt":1771106042484,"changelog":"Initial release of pywayne-aliyun-oss, a Python toolkit for Aliyun OSS file management:\n\n- Upload files, text, images, and directories to OSS buckets.\n- Download individual files or entire directories, supporting prefix-based operations.\n- List keys and directory-like contents with natural sorting.\n- Read file contents, check existence, and retrieve metadata.\n- Delete files individually or by prefix.\n- Copy and move objects within a bucket.\n- Supports both authenticated (read/write) and anonymous (read-only) access.\n- Optional verbose output for operation status messages.","license":null},"metadata":null,"owner":{"handle":"wangyendt","userId":"s172p3g61c0j9vy0sa1bs01b7x885y52","displayName":"wangyendt","image":"https://avatars.githubusercontent.com/u/18455758?v=4"},"moderation":null}