├── LICENSE ├── README.md ├── examples ├── example.py └── text.txt ├── requirements.txt ├── setup.py └── web3storagepy └── __init__.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Tahaa Farooq 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # web3storagepy 2 | This is IPFS web3.storage unofficial library written in python. I have made it simple and easier to integrate with web3.storage API using this masterpiece of codes. 3 | 4 | [![Releases](https://badgen.net/github/releases/tahaafarooq/web3storagepy)](https://github.com/tahaafarooq/web3storagepy/releases) 5 | [![Downloads](https://static.pepy.tech/badge/web3storagepy)](https://pepy.tech/project/web3storagepy) 6 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) 7 | [![Made in Tanzania](https://img.shields.io/badge/made%20in-tanzania-008751.svg?style=flat-square)](https://github.com/Tanzania-Developers-Community/made-in-tanzania) 8 | 9 | ## Installation 10 | You can either install it from git, or using pip. 11 | 12 | ```shell 13 | ~$ git clone https://github.com/tahaafarooq/web3storagepy 14 | ~$ cd web3storagepy 15 | ~$ pip3 install -r requirements.txt 16 | ~$ python3 setup.py install 17 | ``` 18 | 19 | ```shell 20 | ~$ pip3 install web3storagepy 21 | ``` 22 | 23 | ## Usage 24 | 25 | To upload a file to the IPFS web3 storage API we will do the following; 26 | 27 | ```python 28 | >>> import web3storagepy 29 | >> > w3s = web3storagepy 30 | >> > upload = w3s.upload(file="XXXXXX", token="XXXXXXX") 31 | >> > upload 32 | {'STATUS_CODE': 200, 'RESPONSE': '{"cid":"XXXXXXXXXXXXXXXX","carCid":"XXXXXXXXXXXX"}'} 33 | ``` 34 | 35 | Using the CID you are provided with you can decide to check the status of the uploaded file with; 36 | 37 | ```python 38 | >> > import web3storagepy 39 | >> > w3s = web3storagepy 40 | >> > status = w3s.status(cid="XXXXXXXX", token="XXXXXXXXXXXX") 41 | >> > status 42 | {'RESPONSE': '{xxx:xxx}'} 43 | ``` 44 | 45 | You can retrieve all your uploaded files with; 46 | 47 | ```python 48 | >> > import web3storagepy 49 | >> > w3s = web3storagepy 50 | >> > all_files = w3s.user_uploads(token="XXXXXXXXX") 51 | >> > all_files 52 | 'XXXXXXXXXXXXXXXXX' 53 | ``` 54 | 55 | You can access your uploaded file by providing only the CID as follows; 56 | 57 | ```python 58 | >> > import web3storagepy 59 | >> > w3s = web3storagepy 60 | >> > get_file = w3s.get_upload(cid="XXXXXXXXX") 61 | >> > get_file 62 | 'https://XXXXXXXXX.ipfs.w3s.link' 63 | ``` 64 | 65 | ## Give it star 66 | If you happen to find this repository useful or helpful , give it a star! 67 | 68 | ## Issues 69 | 70 | Are you facing any issue with usage of the package, just raise an issue and I will look into fixing it as soon as possible 71 | 72 | ## Contributions 73 | 74 | If there is anything yould would like to add warmly welcome, Just fork it 75 | 76 | ## Disclaimers 77 | 78 | This is not an official package, therefore I'm not responsible for any misinformation or misuse of the package of any kind !!! 79 | 80 | -------------------------------------------------------------------------------- /examples/example.py: -------------------------------------------------------------------------------- 1 | import web3storagepy 2 | 3 | w3s = web3storagepy 4 | 5 | # uploads the file and stores in the web3.storage IPFS system 6 | upload = w3s.upload("text.txt", "eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") 7 | print(upload) 8 | 9 | # check status of the uploaded file using the CID and the token 10 | status = w3s.status("[cid comes here]", "eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") 11 | print(status) 12 | 13 | # list previous uploads 14 | all_uploads = w3s.user_uploads("eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") 15 | print(all_uploads) 16 | 17 | # retrieve an uploaded file with CID 18 | get_file = w3s.get_upload("[cid comes here]") 19 | print(get_file) 20 | 21 | -------------------------------------------------------------------------------- /examples/text.txt: -------------------------------------------------------------------------------- 1 | Hello World 2 | This is a testing file :) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | setuptools==65.5.0 2 | python-magic==0.4.27 3 | requests==2.28.2 -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from os import path 2 | from setuptools import setup 3 | 4 | this_directory = path.abspath(path.dirname(__file__)) 5 | with open(path.join(this_directory, "README.md"), encoding="utf-8") as f: 6 | long_description = f.read() 7 | 8 | setup( 9 | name="web3storagepy", 10 | version="0.0.1", 11 | description="An IPFS web3.storage unofficial library.", 12 | long_description=long_description, 13 | long_description_content_type="text/markdown", 14 | url="https://github.com/tahaafarooq/web3.storage", 15 | download_url="", 16 | author="Tahaa Farooq", 17 | author_email="tahacodez@gmail.com", 18 | license="MIT", 19 | packages=["web3storagepy"], 20 | keywords=[ 21 | "web3 storage", 22 | "web3" "web3 storage api" "IPFS" "web3 api" "IPFS api", 23 | "python-tanzania", 24 | ], 25 | install_requires=["libmagic", "python-magic-bin==0.4.14", "requests"], 26 | include_package_data=True, 27 | python_requires=">=3.7", 28 | classifiers=[ 29 | "Intended Audience :: Developers", 30 | "Topic :: Software Development :: Build Tools", 31 | "License :: OSI Approved :: MIT License", 32 | "Programming Language :: Python :: 3.11", 33 | ], 34 | ) 35 | -------------------------------------------------------------------------------- /web3storagepy/__init__.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import magic 4 | import requests 5 | 6 | 7 | class Web3Storage(object): 8 | def __init__(self): 9 | self.BASE_URL = "https://api.web3.storage/{}" 10 | 11 | # uploads and stores a file 12 | def upload(self, file: str, token: str): 13 | with open(file, "rb") as f: 14 | file_name = os.path.basename(file) 15 | # extension = os.path.splitext(file_name)[1] 16 | mime = magic.Magic(mime=True) 17 | content_type = mime.from_file(file) 18 | headers = { 19 | "Content-Type": content_type, 20 | "Authorization": f"Bearer {token}", 21 | "X-NAME": file_name 22 | } 23 | upload_endpoint = "upload" 24 | req = requests.post(self.BASE_URL.format(upload_endpoint), headers=headers, files={file_name: f}) 25 | if req.status_code == 200: 26 | data = { 27 | "STATUS_CODE": 200, 28 | "RESPONSE": req.text 29 | } 30 | 31 | return data 32 | else: 33 | data = { 34 | "RESPONSE": req.text 35 | } 36 | 37 | return data 38 | 39 | # retrieve information about an upload 40 | def status(self, cid: str, token: str): 41 | headers = { 42 | "Authorization": f"Bearer {token}" 43 | } 44 | status_endpoint = f"status/{cid}" 45 | req = requests.get(self.BASE_URL.format(status_endpoint), headers=headers) 46 | 47 | if req.status_code == 200: 48 | data = { 49 | "STATUS_CODE": 200, 50 | "RESPONSE": req.text 51 | } 52 | 53 | return data 54 | else: 55 | data = { 56 | "RESPONSE": req.text 57 | } 58 | 59 | return data 60 | 61 | # list previous uploads 62 | def user_uploads(self, token: str): 63 | headers = { 64 | "Authorization": f"Bearer {token}" 65 | } 66 | uploads_endpoint = "user/uploads" 67 | req = requests.get(self.BASE_URL.format(uploads_endpoint), headers=headers) 68 | 69 | return req.text 70 | 71 | # returns a single upload 72 | def get_upload(self, cid: str): 73 | # headers = { 74 | # "Accept": "*/*", 75 | # "Authorization": f"Bearer {token}" 76 | # } 77 | # endpoint = f"user/uploads/{cid}" 78 | # req = requests.get(self.BASE_URL.format(endpoint), headers=headers) 79 | # 80 | # return req.text 81 | link = f"https://{cid}.ipfs.w3s.link" 82 | return link 83 | 84 | sys.modules[__name__] = Web3Storage() --------------------------------------------------------------------------------