Skip to content

Instantly share code, notes, and snippets.

@komputronika
Created June 3, 2024 04:35
Show Gist options
  • Select an option

  • Save komputronika/7783be7b64775276ffaa6c6589dfb2bc to your computer and use it in GitHub Desktop.

Select an option

Save komputronika/7783be7b64775276ffaa6c6589dfb2bc to your computer and use it in GitHub Desktop.

Revisions

  1. komputronika created this gist Jun 3, 2024.
    37 changes: 37 additions & 0 deletions put_s3_file.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@
    <?php

    use Aws\S3\S3Client;
    use Aws\S3\Exception\S3Exception;

    $temp_file ="mydoc.pdf";

    $client = new S3Client([
    "version" => "latest",
    "region" => getenv("BIZ_ACCESS_REGION"),
    "endpoint" => "https://" . getenv("BIZ_ENDPOINT"),
    "credentials" => [
    "key" => getenv("BIZ_ACCESS_KEY_ID"),
    "secret" => getenv("BIZ_ACCESS_KEY_SECRET"),
    ],
    ]);

    try {
    $result = $client->putObject(
    [
    "Bucket" => getenv("BIZ_BUCKET_NAME"),
    "Key" => $folder . "/" . $nama_file,

    // <string || resource || Psr\Http\Message\StreamInterface>
    "Body" => fopen($temp_file, "r"),

    // 'private|public-read|public-read-write|authenticated-read|aws-exec-read|bucket-owner-read|bucket-owner-full-control'
    "ACL" => "private",
    ]);
    }
    catch (S3Exception $e)
    {
    echo "Upload failed";
    die();
    }

    ?>