Skip to content

How to download url_private_download file and convert the content to base64 encoding #1283

@PrajwalNaik07

Description

@PrajwalNaik07

I have a slack workflow which takes files input and then calls our slack bot.

I get the url_private_download link in the backend And I tried the following to download the image

`file_path = os.path.join(folder_path, file_name)

    headers = {
        'Authorization': f'Bearer {Slack_bot_token}'
    }

    # Send a GET request to download the file
    response = requests.get(url_private_download, headers)

    if response.status_code == 200:
        # Write the content of the file to the specified location
        with open(file_path, 'wb') as file:
            file.write(response.content)
        log.info(f"File downloaded and saved to {file_path}")

        def encode_file_to_base64(file_path):
            with open(file_path, "rb") as file:
                file_content = file.read()
            encoded_content = base64.b64encode(file_content)  # Encodes to Base64
            encoded_string = encoded_content.decode("utf-8")  # Decode to string format
            return encoded_string

        encoded_file = encode_file_to_base64(file_path)
        # log.info("Base64 Encoded Content:")
        # log.info(encoded_file)
        file_encode[file_name] = encoded_file
    else:
        log.info(f"Failed to download file. Status code: {response.status_code}")`

The file downloaded is corrupted and the base64 encoding is also wrong.

Please let me know how to achieve this

Metadata

Metadata

Labels

questionFurther information is requested

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions