Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save codedeep79/77608729086890f05cca43a88f519b74 to your computer and use it in GitHub Desktop.
Save codedeep79/77608729086890f05cca43a88f519b74 to your computer and use it in GitHub Desktop.
Update text data in files according to specific versions of files with the same name in Minio

To update text data in files according to specific versions of files with the same name in Minio using Python, you can use the Minio Python library to interact with your Minio server. The following steps outline the process:

  1. Install the Minio Python library:
pip install minio
  1. Import the necessary libraries and set up your Minio client:
from minio import Minio
from minio.error import S3Error

minio_client = Minio(
    "your-minio-server-url",
    access_key="your-access-key",
    secret_key="your-secret-key",
    secure=False  # Change to True for SSL
)
  1. List all versions of a specific file in your Minio bucket:
def listFileVersions(bucketName, objectName):
    try:
        versions = minio_client.list_object_versions(bucketName, objectName)
        for version in versions:
            print(f"Version: {version.version_id}, Last Modified: {version.last_modified}")
    except S3Error as e:
        print(f"Error: {e}")
  1. Retrieve a specific version of the file:
def getSpecificVersion(bucketName, objectName, versionID):
    try:
        response = minio_client.get_object(
            bucketName,
            objectName,
            version_id=versionID
        )
        return response.data.decode('utf-8')
    except S3Error as e:
        print(f"Error: {e}")
        return None
  1. Update the text data and put it back into the Minio bucket:
def updateAndPut(bucketName, objectName, versionID, updatedText):
    try:
        minio_client.put_object(
            bucketName,
            objectName,
            io.BytesIO(updatedText.encode('utf-8')),
            len(updatedText),
            content_type='text/plain',
            version_id=versionID
        )
        print("File updated and put back into Minio.")
    except S3Error as e:
        print(f"Error: {e}")

Example

bucketName = "your-bucket-name"
objectName = "your-file.txt"
desiredVersionID = "your-desired-version-id"

# List all versions of the file
listFileVersions(bucketName, objectName)

# Get the specific version you want to update
specificVersionData = getSpecificVersion(bucketName, objectName, desiredVersionID)

if specificVersionData:
    # Modify the text data as needed
    updatedText = specificVersionData.replace("old_text", "new_text")

    # Update and put the modified version back into Minio
    updateAndPut(bucketName, objectName, desiredVersionID, updatedText)

Make sure to replace "your-minio-server-url", "your-access-key", "your-secret-key", "your-bucket-name", "your-file.txt", "your-desired-version-id", and the update logic with your actual values and data modification requirements. This code will help you list versions, retrieve a specific version, update it, and put it back into your Minio bucket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment