我正在尝试使用pinata的API来固定整个目录,因为这是在他们的API documentation中指定的。
我也找到了some python implementation,但这对我来说似乎仍然不起作用。
基本上,我从目录中获取所有文件,并尝试将它们包含在post请求中。
all_files: tp.List[str] = get_all_files(filepath)
files = {"file": [(file, open(file, "rb")) for file in all_files]}
response: requests.Response = requests.post(url=ipfs_url, files=files, headers=headers)我得到的错误如下:
ValueError: too many values to unpack (expected 4)我还尝试了一些硬编码的值,但仍然没有成功:
all_files = ['images/0.png', 'images/1.png', 'images/2.png']
files = {"file": [(file, open(file, "rb")) for file in all_files]}
response: requests.Response = requests.post(url=ipfs_url, files=files, headers=headers)然而,在这种情况下,我得到了一个不同的错误:
TypeError: expected string or bytes-like object同样,作为另一次尝试,我尝试了这样的东西:
files = (
("file", ("0.png", open('images/0.png', "rb"))),
("file", ("1.png", open('images/1.png', "rb")))
)
response: requests.Response = requests.post(url=ipfs_url, files=files, headers=headers)在这种情况下,Pinata无法识别我想要的内容,并返回以下错误:
{'error': 'More than one file and/or directory was provided for pinning.'}有人能给我一些提示吗?我哪里做错了?谢谢!
编辑:添加要使用的完整示例:
import os
import requests
import typing as tp
def get_all_files(directory: str) -> tp.List[str]:
"""get a list of absolute paths to every file located in the directory"""
paths: tp.List[str] = []
for root, dirs, files_ in os.walk(os.path.abspath(directory)):
for file in files_:
paths.append(os.path.join(root, file))
return paths
print("hello")
# Example 1
all_files: tp.List[str] = get_all_files("images")
files = {"file": [(file, open(file, "rb")) for file in all_files]}
# Example 2
#all_files = ['images/0.png', 'images/1.png', 'images/2.png']
#files = {"file": [(file, open(file, "rb")) for file in all_files]}
# Example 3
#files = (
# ("file", ("0.png", open('images/0.png', "rb"))),
# ("file", ("1.png", open('images/1.png', "rb")))
#)
print ("Files used: \n")
print (files)
headers = {
'pinata_api_key': "",
'pinata_secret_api_key': ""
}
ipfs_url = "https://api.pinata.cloud/pinning/pinFileToIPFS"
response: requests.Response = requests.post(url=ipfs_url, files=files, headers=headers)
print(response.json())发布于 2021-09-07 17:23:53
好的,我设法上传了整个目录,并指定了如下文件:
files = [
('file', ('images/0.png', open('images/0.png', "rb"))),
('file', ('images/1.png', open('images/1.png', "rb"))),
]因此,总的来说,为了收集所有文件并上传它们,我能够使用以下代码:
all_files: tp.List[str] = get_all_files(directory)
files = [('file', (file, open(file, "rb"))) for file in all_files]https://stackoverflow.com/questions/69086493
复制相似问题