2012-02-04 13 views
21

Sto usando questo per sempre con graffetta e AWS-s3:AWS :: S3 :: S3Object.url_for - Come fare questo con la nuova gemma SDK AWS?

def authenticated_url(style = nil, expires_in = 90.minutes) 
     AWS::S3::S3Object.url_for(attachment.path(style || attachment.default_style), attachment.bucket_name, :expires_in => expires_in, :use_ssl => true) 
    end 

La nuova graffetta utilizza la gemma AWS-SDK, che rompe questo dà l'errore:

undefined method `url_for' for AWS::S3:Class 

Qualcuno sa come ottenere questo metodo per lavorare con la nuova gemma AWS-SDK?

risposta

29

per generare un URL utilizzando la gemma AWS-sdk si dovrebbe utilizzare il metodo AWS::S3Object#url_for.
È possibile accedere all'istanza S3Object da un allegato a forma di graffetta usando # s3_object. Lo snippet riportato di seguito dovrebbe risolvere il problema.

def authenticated_url(style = nil, expires_in = 90.minutes) 
    attachment.s3_object(style).url_for(:read, :secure => true, :expires => expires_in).to_s 
end 
+0

S3Object # url_for restituisce un oggetto URI :: HTTPS. Se preferisci questo puoi omettere i #to_s dalla catena del metodo. –

+0

AWS :: S3 :: Base è una classe all'interno della vecchia gemma aws-s3, ma non esiste come parte della gemma aws-sdk. Entrambe le gemme definiscono la classe AWS :: S3. Scavare attraverso la traccia dello stack e scoprire cosa si riferisce a AWS :: S3 :: Base. –

5

Dopo aver esaminato il numero documentation, url_for è un metodo di istanza e non un metodo di classe.

per generare un URL con AWS-sdk, è necessario effettuare le seguenti operazioni:

bucket = AWS::S3::Bucket.new(attachment.bucket_name) 
s3object = AWS::S3::S3Object.new(bucket, attachment.path(style || attachment.default_style)) 
s3object.url_for(:read, :expires => expires_in) 

Le opzioni sono leggermente diverse da quelle specificate.

Options Hash (options):

:expires (Object) — Sets the expiration time of the URL; after this time S3 will return an error if the URL is used. This can be an integer (to specify the number of seconds after the current time), a string (which is parsed as a date using Time#parse), a Time, or a DateTime object. This option defaults to one hour after the current time.

:secure (String) — Whether to generate a secure (HTTPS) URL or a plain HTTP url.

:response_content_type (String) — Sets the Content-Type header of the response when performing an HTTP GET on the returned URL.

:response_content_language (String) — Sets the Content-Language header of the response when performing an HTTP GET on the returned URL.

:response_expires (String) — Sets the Expires header of the response when performing an HTTP GET on the returned URL.

:response_cache_control (String) — Sets the Cache-Control header of the response when performing an HTTP GET on the returned URL.

:response_content_disposition (String) — Sets the Content-Disposition header of the response when performing an HTTP GET on the returned URL.

:response_content_encoding (String) — Sets the Content-Encoding header of the response when performing an HTTP GET on the returned URL.

+0

"modifica negli commit"? Quindi, come dovrei generare un url con la gemma aws-sdk? – AnApprentice

+0

@AnApprentice Vedi la mia modifica. Inoltre, quale versione della gemma ha funzionato per il tuo metodo? – Gazler

+0

@AnApprentice Ho editato di nuovo per riflettere il codice di esempio. – Gazler

3

Ho recentemente fatto il passaggio da aws-s3 AWS-sdk pure. Ho sostituito tutto il mio url_for con il seguente:

def authenticated_url(style = nil, expires_in = 90.minutes) 
    self.attachment.expiring_url(expires_in, (style || attachment.default_style)) 
end 

Si può vedere la discussione delle questioni graffetta thread qui: https://github.com/thoughtbot/paperclip/issues/732

+0

Che errori ancora – AnApprentice

+0

Guarda la definizione del metodo qui: https://github.com/thoughtbot/paperclip/blob/master/lib/paperclip/storage/s3.rb. È la stessa della tua risposta accettata. L'unica ragione per cui ho potuto vederlo non funziona se non si dispone di un file s3.yml con la chiave di accesso e la chiave segreta o se non si specifica s3 nelle opzioni di paperclip. Che errore stai ottenendo? – John

9

Recentemente ho aggiornato alla più recente gioiello per AWS SDK 2 per Ruby (AWS-sdk -2.1.13) e l'aggiornamento dell'URL preconfigurato è stato modificato in questa versione dell'SDK.

Il modo di ottenerlo:

presigner = Aws::S3::Presigner.new 
presigner.presigned_url(:get_object, #method 
         bucket: 'bucket-name', #name of the bucket 
         key: "key-name", #key name 
         expires_in: 7.days.to_i #time should be in seconds 
         ).to_s 

È possibile trovare maggiori informazioni qui: http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Presigner.html

+1

Ho trovato che Aws :: S3 :: ObjectSummary risponde anche a 'presigned_url', e non avevo bisogno di un oggetto Aws :: S3 :: Presigner. I dettagli nei commenti sono d'oro, però, grazie! – pdobb

Problemi correlati