3

I'm trying to upload a directory of pngs to pinata, but I keep getting this error:

{'error': 'Invalid file path: C:\\Users\\myname\\Documents\\Coding Projects\\nft\\art\\reveal\\1.png detected. 
If you are receiving this, you provided either bad data or a file path our systems have 
flagged as unsafe for processing. If your file paths are valid, please contact us at
team@pinata.cloud. We would love to help accommodate your needs!'}

Here is the code that I've tried:


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

def upload_directory_to_pinata(directory):

    all_files: tp.List[str] = get_all_files(directory)
    files = [("file", (file, open(file, "rb"))) for file in all_files]

    print(files)

    headers = {
        "pinata_api_key": os.getenv("PINATA_API_KEY"),
        "pinata_secret_api_key": os.getenv("PINATA_API_SECRET"),
    }

    response = requests.Response = requests.post(
        url=PINATA_BASE_URL + endpoint, files=files, headers=headers
    )

    data = response.json()
    print(data)
    imageLinkBase = "ipfs://" + data["IpfsHash"] + "/"
    return imageLinkBase

That filepath is indeed a valid filepath. I need to have a IPFS (or pinata) directory with a CID and then a bunch of number pictures in there. If there is any other way to do this, lmk! Thank you!

frankied003
  • 466
  • 6
  • 26

1 Answers1

1

Perhaps you added the absolute path to each file instead of the relative file path?

This endpoint also allows users to pin an entire directory to IPFS. 
This works almost identically to pinning a file, with the main difference being that we provide 
an array of files and need to provide a relative file path for each file in the directory.

However, our servers will use the exact path that's provided for each file, 
so it's important that each path begins with the "base" directory that is being uploaded. 
As an example, if your directory is located at "./../myBuilds/desiredBuild" 
on your local machine, then each file path should start with "desiredBuild".