Click here to Skip to main content
16,022,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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-swiftc...t-api.html.

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 sanbox_nb01_netbox_2024-07-20.psql.gz

gzip: sanbox_nb01_netbox_2024-07-20.psql.gz: not in gzip format
>

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

Any assistance will be appreciated.

Yours sincerely

What I have tried:

Below is the code I wrote to upload the compressed file (.gz)

Python
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-07-20.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,
        "sanbox_nb01_netbox_2024-07-20.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)



Below is the code I wrote to upload the compressed file (.gz)

Python
import gzip
import shutil
import tarfile

# 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 session
keystone_session = session.Session(auth=auth)

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

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

# Download the created object from the Object Storage
obj = 'sanbox_nb01_netbox_2024-07-20.psql.gz'
container = 'netbox-backups'
resp_headers, obj_contents = swift_conn.get_object(container, obj)
with open('sanbox_netbox_2024-07-20.psql.gz', 'wb') as local:
local.write(obj_contents)
Posted
Updated 25-Jul-24 3:51am
v5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900