I wrote the Python code to upload the .gz file from my local machine to the OpenStack object store using the following documentation: https://docs.openstack.org/python-swiftclient/latest/client-api.html.
Below is the code I wrote.

from keystoneauth1 import session
    from keystoneauth1.identity import v3
    from swiftclient.client import Connection, logger
    from swiftclient.client import ClientException
    import gzip

    # Create a password auth plugin
    auth = v3.Password(
        auth_url='https://cloud.company.com:5000/v3/',
        username='myaccount',
        password='mypassword',
        user_domain_name='Default',
        project_name='myproject',
        project_domain_name='Default'
    )

    # Create swiftclient Connection
    swift_conn = Connection(session=keystone_session)

    # Create a new container
    container = 'object-backups'
    swift_conn.put_container(container)
    res_headers, containers = swift_conn.get_account()
    if container in containers:
        print("The container " + container + " was created!")

    # Create a new object with the contents of Netbox database backup
    with gzip.open('/var/backup/netbox_backups/netbox_2024-03-16.psql.gz', 'rb') as f:
        # Read the contents...
        file_gz_content = f.read()

        # Upload the returned contents to the Swift Object Storage container
        swift_conn.put_object(
            container,
            "object_netbox_2024-06-16.psql.gz",
            contents=file_gz_content,
            content_type='application/gzip'
        )

    # Confirm the presence of the object holding the Netbox database backup
    obj1 = 'object_netbox_2024-06-16.psql.gz'
    container = 'object-backups'
    try:
        resp_headers = swift_conn.head_object(container, obj1)
        print("The object " + obj1 + " was successfully created")
    except ClientException as e:
        if e.http_status == '404':
            print("The object " + obj1 + " was not found!")
        else:
            print("An error occurred checking for the existence of the object " + obj1)

The file gets uploaded successfully. However, if I download the file from the object store and try to decompress it, I get the following error:

# gzip -d object_netbox_2024-06-16.psql.gz 

gzip: sanbox_nb01_netbox_2024-06-16.psql.gz: not in gzip format

What should I do to ensure the file gets downloaded in the same format and size to the Object storage as the file in my local machine?

Any assistance will be appreciated.

Yours sincerely

Check the type of compression using the file command:

file name_name.tgz

This is the compression type of the original file in my local machine:

file netbox_2024-07-09.psql.gz 
netbox_2024-07-09.psql.gz: gzip compressed data, last modified: Tue Jul  9 07:56:02 2024, from Unix, original size 1875639

Below is the compression type of the file downloaded from the OpenStack Object Storage

file sanbox_nb01_netbox_2024-07-09.psql.gz 
sanbox_nb01_netbox_2024-07-09.psql.gz: UTF-8 Unicode text, with very long lines

How do I ensure that both files have the same compression type? Because I made sure in my code that its content type is application/gzip

I can't tell where the file is altered but it appears the file is indeed being altered. To figure out where I would be using the file command on the server as well. But I can't tell if you can shell into said server.

No, unfortunately. I can communicate with the server through the swift command since it's an OpenStack Object store. If I use the Swift command, the uploaded file is in the same format and size as the one on my local machine. I wanted to automate this process using a Python script.

I will try again to convey how I would troubleshoot this.

Now that you revealed that "it works" with the Swift command but not Python you still need to find out where the file alteration happens. To wit, I would see if the file made it to the server intact first. Use your code to send the file up and the Swift commands to check the file with the file command and to copy it back down. At this point you can tell if the issue was on the upload or download. RIGHT NOW you don't know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.