Reply To: Gravity View compatibility?

Reply
Gary Michael
Support Manager

If you’re comfortable with code, here is a function that will be included in the next version of SyncS3. You can use it now to retrieve all of the S3 URLs for an entry.

To use it, you pass in a Gravity Forms Entry ID as the first parameter, and whether to pre-sign the URLs as the second parameter (true or false). It returns a multi-dimensional array of the URLs that you can loop through to show the URLs.

if ( ! defined( 'syncs3_get_entry_s3_urls' ) ) :
	/**
	 * Gets the S3 URLs of an entry
	 *
	 * @since 1.4.2
	 *
	 * @param  mixed 	$entry 		Entry or Entry ID
	 * @param  boolean 	$entry_id 	Whether to sign the URL (needed to access a protected file)
	 *
	 * @return array
	 */
	function syncs3_get_entry_s3_urls( $entry, $presigned = false ) {
		$entry_id = is_array( $entry ) && ! empty( $entry['id'] ) ? (int) $entry['id'] : (int) $entry;
		if ( empty( $entry_id ) ) {
			return;
		}
		$s3_urls = gform_get_meta( $entry_id, 's3_urls' );
		if ( empty( $s3_urls ) ) {
			return;
		}
		$returned_urls = array();
		foreach ( $s3_urls as $field_id => $urls ) {
			foreach ( $urls as $index => $url ) {
				if ( is_array( $url ) && isset( $url['file_url'] ) ) {
					if ( $presigned ) {
						$client_config = array(
							'version' => 'latest',
							'region' => $url['region'],
							'credentials' => array(
								'key' => $url['access_key'],
								'secret' => $url['secret_key'],
							),
						);
						if ( ! empty( $url['endpoint'] ) ) {
							$client_config['endpoint'] = $url['endpoint'];
						}
						$s3 = syncs3_s3_client( $client_config );
						$cmd = $s3->getCommand( 'GetObject', [
						    'Bucket' => $url['bucket'],
						    'Key' => $url['key']
						]);
						$request = $s3->createPresignedRequest( $cmd, apply_filters( 'syncs3_get_entry_s3_urls_presigned_link_time', '+20 minutes', $url, $entry_id, $field_id ) );
						$returned_urls[$field_id][$index]['signed'] = (string) $request->getUri();
					}
					$returned_urls[$field_id][$index]['unsigned'] = $url['file_url'];
				} else {
					$returned_urls[$field_id][$index]['unsigned'] = $url;
				}
			}
		}
		return $returned_urls;
	}
endif;

Example use:

$s3_urls = syncs3_get_entry_s3_urls( $entry_id, true );
foreach ( $s3_urls as $field_id => $urls ) {
	foreach ( $urls as $url ) {
		$unsigned = $url['unsigned'];
		$signed = $url['signed'];
	}
}
Your Cart
Your cart is currently empty.
Open Cart