Python Forum
How can I upload a .gz file to the Swift Object Storage using Python swift client?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I upload a .gz file to the Swift Object Storage using Python swift client?
#1
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, when I tried to decompress the file after downloading it from the object storage using the Swift commands (I cannot ssh to the object storage instance apart from using Swift commands), I got the following

Output:
# gzip -d object_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?


Below is the code I wrote to upload the file to the Swift object storage

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)
Below is the code I wrote to download the file from the Swift object storage to my local machine

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)
Any assistance will be appreciated.

Yours sincerely
Gribouillis write Jul-22-2024, 02:47 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  client A to server B connects but client B to server A doesnt connect gerald 1 843 Aug-17-2024, 07:33 AM
Last Post: Gribouillis
  Error (Errno 2), File upload with the Flask framework and a public IP Username_Python1 0 1,353 Mar-28-2024, 01:46 PM
Last Post: Username_Python1
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 1,420 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Upload Files to Azure Storage Container phillyfa 6 3,261 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  Making a question answering chatbot based on the files I upload into python. Joejones 1 3,904 May-19-2023, 03:09 PM
Last Post: deanhystad
  '' FTP '' File upload with a specified string and rename midomarc 1 2,295 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  Networking Issues - Python GUI client and server connection always freezes Veritas_Vos_Liberabit24 0 1,453 Mar-21-2023, 03:18 AM
Last Post: Veritas_Vos_Liberabit24
  Getting "SSL client not supported by this Python installation" error prabirsarkar 0 1,658 Mar-13-2023, 05:01 PM
Last Post: prabirsarkar
  python update binary object (override delivered Object properties) pierre38 4 2,911 May-19-2022, 07:52 AM
Last Post: pierre38
  Upload image to Instagram using python/selenium using image URL failed, need help greenpine 5 8,046 Feb-22-2022, 09:42 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020