skip to Main Content

How do I grab an image from an amazon with a format like this:

https://platform.s3.us-west-2.amazonaws.com/listing/31767-84007-dwBVIaLkhsGzpK4sfxaS-T07Gd-EXAd78l8lsJpeuYM-618cf68ce2350

I got this data from a Booking Engine API, and there’s no other way to get a different result.

I am using WordPress and "media_sideload_image" doesn’t accept it as a valid URL.

2

Answers


  1. Same Issues was for me, I made the file public I was able to access my image as https://metapyxl.s3.amazonaws.com/images/1696425186798_1.png

    So you need to make your image public on S3 bucket.

    I attached images to update permission on S3 bucket.

    Allow public access

    Allow Bucket policy

    Login or Signup to reply.
  2. require_once ABSPATH . 'wp-admin/includes/media.php';
    require_once ABSPATH . 'wp-admin/includes/file.php';
    require_once ABSPATH . 'wp-admin/includes/image.php';
    
    function upload_image_from_url($image_url) {
        // Check if the URL is valid
        if (filter_var($image_url, FILTER_VALIDATE_URL)) {
            // Get the file type
            $file_type = wp_check_filetype(basename($image_url), null);
    
            // Prepare an array of data for the attachment
            $attachment = array(
                'post_title'     => sanitize_file_name(basename($image_url)),
                'post_mime_type' => $file_type['type'],
            );
    
            // Try to upload the image
            $attachment_id = media_sideload_image($image_url, 0, null, 'id');
    
            if (!is_wp_error($attachment_id)) {
                // The image was successfully uploaded, and $attachment_id contains the attachment ID.
                return $attachment_id;
            } else {
                // An error occurred during the upload.
                return false;
            }
        }
    
        return false;
    }
    
    // Example usage:
    $image_url = 'https://metapyxl.s3.amazonaws.com/images/1696425186798_1.png';
    $attachment_id = upload_image_from_url($image_url);
    
    if ($attachment_id) {
        // The image was uploaded successfully. You can use $attachment_id as needed.
    } else {
        // There was an error uploading the image.
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search