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.1.tar.gz (7.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.1-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: teknohole-1.1.1.tar.gz
  • Upload date:
  • Size: 7.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.1.tar.gz
Algorithm Hash digest
SHA256 a44fc437ca58ca23c45d80d7281e904b512db8ed678fec299cbd3d1ca9423bd4
MD5 386c25510fce529632a0dd36fe558e86
BLAKE2b-256 a7797f09aef0c69add2b1b4c3b4385dd496e38fee022835c285c1244838da74f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: teknohole-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 7.3 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 32321d9a98b77ec040be389b554a6d75700780acf23ba49bf204f374dea428cb
MD5 4b9444553414af88c8f486303ee1cb30
BLAKE2b-256 ddcf79ec8e6b11fa6b25bd2ac29f94762a0ffc4fa0797e63637d58d73a55d5cf

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