-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b31874c
commit eb304d2
Showing
7 changed files
with
239 additions
and
674 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,77 @@ | ||
# web3.storage | ||
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. | ||
|
||
[](https://github.com/tahaafarooq/web3.storage) | ||
[](https://opensource.org/licenses/MIT) | ||
|
||
## Installation | ||
You can either install it from git, or using pip. | ||
|
||
```shell | ||
~$ git clone https://github.com/tahaafarooq/web3.storage | ||
~$ cd web3.storage | ||
~$ pip3 install -r requirements.txt | ||
~$ python3 setup.py install | ||
``` | ||
|
||
```shell | ||
~$ pip3 install web3storagepy | ||
``` | ||
|
||
## Usage | ||
|
||
To upload a file to the IPFS web3 storage API we will do the following; | ||
|
||
```python | ||
>> > import web3storagepy | ||
>> > w3s = web3storagepy | ||
>> > upload = w3s.upload(file="XXXXXX", token="XXXXXXX") | ||
>> > upload | ||
{'STATUS_CODE': 200, 'RESPONSE': '{"cid":"XXXXXXXXXXXXXXXX","carCid":"XXXXXXXXXXXX"}'} | ||
``` | ||
|
||
Using the CID you are provided with you can decide to check the status of the uploaded file with; | ||
|
||
```python | ||
>> > import web3storagepy | ||
>> > w3s = web3storagepy | ||
>> > status = w3s.status(cid="XXXXXXXX", token="XXXXXXXXXXXX") | ||
>> > status | ||
{'RESPONSE': '{xxx:xxx}'} | ||
``` | ||
|
||
You can retrieve all your uploaded files with; | ||
|
||
```python | ||
>> > import web3storagepy | ||
>> > w3s = web3storagepy | ||
>> > all_files = w3s.user_uploads(token="XXXXXXXXX") | ||
>> > all_files | ||
'XXXXXXXXXXXXXXXXX' | ||
``` | ||
|
||
You can access your uploaded file by providing only the CID as follows; | ||
|
||
```python | ||
>> > import web3storagepy | ||
>> > w3s = web3storagepy | ||
>> > get_file = w3s.get_upload(cid="XXXXXXXXX") | ||
>> > get_file | ||
'https://XXXXXXXXX.ipfs.w3s.link' | ||
``` | ||
|
||
## Give it star | ||
If you happen to find this repository useful or helpful , give it a star! | ||
|
||
## Issues | ||
|
||
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 | ||
|
||
## Contributions | ||
|
||
If there is anything yould would like to add warmly welcome, Just fork it | ||
|
||
## Disclaimers | ||
|
||
This is not an official package, therefore I'm not responsible for any misinformation or misuse of the package of any kind !!! | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import web3storagepy | ||
|
||
w3s = web3storagepy | ||
|
||
# uploads the file and stores in the web3.storage IPFS system | ||
upload = w3s.upload("text.txt", "eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") | ||
print(upload) | ||
|
||
# check status of the uploaded file using the CID and the token | ||
status = w3s.status("[cid comes here]", "eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") | ||
print(status) | ||
|
||
# list previous uploads | ||
all_uploads = w3s.user_uploads("eyJhbGci[REDACTED]pXVCJ9.eyJzdWIiOiJkaWQ6ZXRoc[REDACTED]FiZDdkMDVlZDgwMEI5RUIiL{REDACTED}3JhZ2UiLCJpYXQiOjE2Njk1N[REDACTED]ZDHKqSUZ8_k2C0NwQn0Oc") | ||
print(all_uploads) | ||
|
||
# retrieve an uploaded file with CID | ||
get_file = w3s.get_upload("[cid comes here]") | ||
print(get_file) | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Hello World | ||
This is a testing file :) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
setuptools==65.5.0 | ||
python-magic==0.4.27 | ||
requests==2.28.2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from os import path | ||
from setuptools import setup | ||
|
||
this_directory = path.abspath(path.dirname(__file__)) | ||
with open(path.join(this_directory, "README.md"), encoding="utf-8") as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name="web3storagepy", | ||
version="0.0.1", | ||
description="An IPFS web3.storage unofficial library.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
url="https://github.com/tahaafarooq/web3.storage", | ||
download_url="", | ||
author="Tahaa Farooq", | ||
author_email="tahacodez@gmail.com", | ||
license="MIT", | ||
packages=["web3storagepy"], | ||
keywords=[ | ||
"web3 storage", | ||
"web3" "web3 storage api" "IPFS" "web3 api" "IPFS api", | ||
"python-tanzania", | ||
], | ||
install_requires=["libmagic", "python-magic-bin==0.4.14", "requests"], | ||
include_package_data=True, | ||
python_requires=">=3.7", | ||
classifiers=[ | ||
"Intended Audience :: Developers", | ||
"Topic :: Software Development :: Build Tools", | ||
"License :: OSI Approved :: MIT License", | ||
"Programming Language :: Python :: 3.11", | ||
], | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import os | ||
import sys | ||
import magic | ||
import requests | ||
|
||
|
||
class Web3Storage(object): | ||
def __init__(self): | ||
self.BASE_URL = "https://api.web3.storage/{}" | ||
|
||
# uploads and stores a file | ||
def upload(self, file: str, token: str): | ||
with open(file, "rb") as f: | ||
file_name = os.path.basename(file) | ||
# extension = os.path.splitext(file_name)[1] | ||
mime = magic.Magic(mime=True) | ||
content_type = mime.from_file(file) | ||
headers = { | ||
"Content-Type": content_type, | ||
"Authorization": f"Bearer {token}", | ||
"X-NAME": file_name | ||
} | ||
upload_endpoint = "upload" | ||
req = requests.post(self.BASE_URL.format(upload_endpoint), headers=headers, files={file_name: f}) | ||
if req.status_code == 200: | ||
data = { | ||
"STATUS_CODE": 200, | ||
"RESPONSE": req.text | ||
} | ||
|
||
return data | ||
else: | ||
data = { | ||
"RESPONSE": req.text | ||
} | ||
|
||
return data | ||
|
||
# retrieve information about an upload | ||
def status(self, cid: str, token: str): | ||
headers = { | ||
"Authorization": f"Bearer {token}" | ||
} | ||
status_endpoint = f"status/{cid}" | ||
req = requests.get(self.BASE_URL.format(status_endpoint), headers=headers) | ||
|
||
if req.status_code == 200: | ||
data = { | ||
"STATUS_CODE": 200, | ||
"RESPONSE": req.text | ||
} | ||
|
||
return data | ||
else: | ||
data = { | ||
"RESPONSE": req.text | ||
} | ||
|
||
return data | ||
|
||
# list previous uploads | ||
def user_uploads(self, token: str): | ||
headers = { | ||
"Authorization": f"Bearer {token}" | ||
} | ||
uploads_endpoint = "user/uploads" | ||
req = requests.get(self.BASE_URL.format(uploads_endpoint), headers=headers) | ||
|
||
return req.text | ||
|
||
# returns a single upload | ||
def get_upload(self, cid: str): | ||
# headers = { | ||
# "Accept": "*/*", | ||
# "Authorization": f"Bearer {token}" | ||
# } | ||
# endpoint = f"user/uploads/{cid}" | ||
# req = requests.get(self.BASE_URL.format(endpoint), headers=headers) | ||
# | ||
# return req.text | ||
link = f"https://{cid}.ipfs.w3s.link" | ||
return link | ||
|
||
sys.modules[__name__] = Web3Storage() |