Skip to main content

Package CLI resmi dari teknohole.com

Project description

Package resmi teknohole.com

Penggunaan WebStorage SDK

Dokumentasi ini menjelaskan cara menggunakan WebStorage SDK untuk mengunggah dan menghapus file.


1. Instalasi

pip install teknohole

2. Inisialisasi Klien

Pertama, impor kelas WebStorage dan buat sebuah instance dengan API Key dan Nama Storage Anda (didapat dari service web storage teknohole).

from teknohole.web import WebStorage

# Ganti dengan kredensial Anda
api_key = "API_KEY_ANDA_YANG_SANGAT_RAHASIA"
storage_name = "NAMA_STORAGE_ANDA"

# Buat instance klien
client = WebStorage(api_key, storage_name)

3. Mengunggah File 🖼️

Gunakan metode upload_file() untuk mengunggah file. Metode ini memerlukan satu argumen: path (lokasi) lengkap ke file yang ingin Anda unggah.

Contoh Penggunaan

# Asumsikan Anda memiliki file bernama 'gambar-produk.jpg' di direktori yang sama
file_path = 'gambar-produk.jpg'

print(f"Mencoba mengunggah file: {file_path}")
hasil_upload = client.upload_file(file_path)

if hasil_upload['success']:
    # Jika berhasil, simpan object_key untuk penggunaan di masa depan (misal: delete)
    object_key = hasil_upload['data']['key']
    print(f"✅ Upload Berhasil!")
    print(f"   Pesan: {hasil_upload['message']}")
    print(f"   Object Key: {object_key}")
else:
    # Jika gagal
    print(f"❌ Upload Gagal!")
    print(f"   Pesan: {hasil_upload['message']}")

# Jangan lupa untuk menyimpan object_key di database Anda jika perlu

Respons dari upload_file()

  • Jika Berhasil: Anda akan menerima dictionary dengan success: True dan sebuah data yang berisi key dari objek yang diunggah.

    {
        "success": true,
        "message": "File berhasil diunggah.",
        "data": {
            "key": "https://cdn.teknohole.com/<id-akun>/<nama-storage>/<nama-file>"
        }
    }
    
  • Jika Gagal: Anda akan menerima dictionary dengan success: False dan sebuah message yang menjelaskan penyebab kegagalan.

    {
        "success": false,
        "message": "File tidak ditemukan: gambar-produk.jpg"
    }
    

Modifikasi File

Anda juga bisa memodikasi file dengan cara upload file dengan nama yang sama melalui sdk, tetapi ini akan memerlukan waktu agar file berubah sepenuhnya di CDN.


4. Menghapus File

Gunakan metode delete_file() untuk menghapus file. Metode ini memerlukan object_key yang Anda dapatkan saat berhasil mengunggah file.

Contoh Penggunaan

# Gunakan object_key yang didapat dari proses upload
key_untuk_dihapus = "https://cdn.teknohole.com/<id-akun>/<nama-storage>/<nama-file>" 

print(f"Mencoba menghapus file dengan key: {key_untuk_dihapus}")
hasil_hapus = client.delete_file(key_untuk_dihapus)

if hasil_hapus['success']:
    print(f"✅ File Berhasil Dihapus!")
else:
    print(f"❌ Gagal Menghapus File!")
    print(f"   Pesan: {hasil_hapus['message']}")

5. Menutup Koneksi (Penting!)

Setelah selesai menggunakan klien, sangat disarankan untuk menutup koneksi menggunakan metode close(). Ini akan membebaskan sumber daya jaringan. Praktik terbaik adalah menggunakan blok try...finally untuk memastikan koneksi selalu ditutup, bahkan jika terjadi error.

Contoh Praktik Terbaik

from web_client_simple import WebStorage
import os

api_key = "API_KEY_ANDA"
storage_name = "NAMA_STORAGE_ANDA"
client = WebStorage(api_key, storage_name)

# Membuat file dummy untuk diuji
with open("test_file.txt", "w") as f:
    f.write("Ini adalah file tes.")

try:
    # Lakukan semua operasi di dalam blok 'try'
    print("--- Proses Upload ---")
    upload_result = client.upload_file("test_file.txt")
    
    if upload_result['success']:
        print(f"Upload berhasil. Key: {upload_result['data']['key']}")
        
        object_key = upload_result['data']['key']
        
        print("\n--- Proses Delete ---")
        delete_result = client.delete_file(object_key)
        
        if delete_result['success']:
            print("Penghapusan berhasil.")
        else:
            print(f"Penghapusan gagal: {delete_result['message']}")
            
    else:
        print(f"Upload gagal: {upload_result['message']}")

finally:
    # Blok 'finally' akan selalu dieksekusi
    print("\nMenutup koneksi klien...")
    client.close()
    
    # Menghapus file dummy
    os.remove("test_file.txt")
    print("Selesai.")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teknohole-1.1.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teknohole-1.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file teknohole-1.1.0.tar.gz.

File metadata

  • Download URL: teknohole-1.1.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for teknohole-1.1.0.tar.gz
Algorithm Hash digest
SHA256 da45f4819ca30eaf9328a582caa62ef4a169afc6b5683ee370685cc4e22793ad
MD5 3b47d8ba799726d2071e3a777719315c
BLAKE2b-256 542f10f15a6d5df5f91790d85b18de9f5a6a45fac37b8bc7f83ecf21c2b9a97c

See more details on using hashes here.

File details

Details for the file teknohole-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: teknohole-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for teknohole-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef7baf10597674b865b6bfed6d644553f861a864f1d81f73b33e738de8fbbac3
MD5 0c7039eef1543520e71373a5bac895c5
BLAKE2b-256 b956f99c53e8f3fdecb938a5c3af8191cbe57a948956c409c8ded24db67e7a03

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page