<p>So this works. Now that I have the understanding of how things fire I can clean this up. I am open to better solutions.</p>

<pre><code>    craft()-&gt;on('entries.onSaveEntry', function(Event $event)
    {   

        $entry = $event-&gt;params['entry'];    
        if($event-&gt;params['isNewEntry'] &amp;&amp; $entry-&gt;getSection() == 'Episode')

        {

        require_once(craft()-&gt;path-&gt;getPluginsPath().'cranberry/vendor/soundcloud/autoload.php');

        $clientID       =   'xxxxxxxxxxxxxxx';
        $clientSecret   =   'xxxxxxx';
        $username       =   'xxxx';
        $password       =   'xxxx';

        $facade = new \Njasm\Soundcloud\SoundcloudFacade($clientID, $clientSecret);
        $facade-&gt;userCredentials($username, $password); 

        $mp3File = $entry-&gt;mp3File-&gt;first()-&gt;filename;
        $url = $entry-&gt;mp3File-&gt;first()-&gt;getUrl();

        $mp3TempFile = "/var/www/pathway/craft/storage/tmp/".$mp3File;

        // download the file from amazon s3 and push it to soundcloud
        $fp = file_put_contents($mp3TempFile, fopen($url, 'r'));

        $params = array(
            'track[title]' =&gt; iconv('UTF-8', 'ASCII//TRANSLIT', $entry-&gt;title),
            'track[sharing]' =&gt; 'public',
            //'track[asset_data]' =&gt; $mp3Filepath,
            'track[description]' =&gt; iconv('UTF-8', 'ASCII//TRANSLIT', strip_tags($entry-&gt;summary)),
            'track[track_type]' =&gt; 'podcast',
            'track[genre]' =&gt; 'Learning',
            'track[license]' =&gt; 'all-rights-reserved'
            );

        $response = $facade-&gt;upload($mp3TempFile, $params);
        $fp = unlink($mp3TempFile);

        } 

    });
</code></pre>

CC BY-SA 3.0
2016-04-19T15:48:34.860Z
BTW, if anyone knows a craft path that gets me here /var/www/pathway/craft/storage, (something similar to craft()->path->getPluginsPath()) that would be helpful.
David A McInnis
CC BY-SA 3.0
2016-04-19T18:07:14.423Z
The [``CRAFT_STORAGE_PATH``](https://craftcms.com/docs/php-constants#path-constants)  constant should do the trick?
RitterKnight
CC BY-SA 3.0
2016-04-18T23:42:56.727Z
2016-04-18T23:42:56.727Z
David A McInnis
<p>Is it possible to grab file (mp3) that has been submitted as part of an entry and post it to a secondary location (Soundcloud via their API) before it gets stored to my asset source at Amazon S3? </p>

<p>To complicate things a bit, I am uploading multiple files per entry, each one having its own field. One is the produced file (field name mp3File) selection) and the others are the raw recordings (field name rawFiles). </p>

<p>It is produced file (mp3File) that I am wanting to push to SoundCloud with their API. </p>

<p>I have a handle on how the SoundCloud API works, what I need is to pass the file path to the SoundCloud API. I think it is probably better to do this while the file still resides on the server before it makes its way to Amazon. Or it could be that I don't understand the flow here at all. </p>

<plugin-development><assets><amazon>
assets.beforeSaveEntry to upload file to secondary location in addition to Amazon S3
# assets.beforeSaveEntry to upload file to secondary location in addition to Amazon S3

(Asked by: username_0 on 2016-04-12)

Is it possible to grab file (mp3) that has been submitted as part of an entry and post it to a secondary location (Soundcloud via their API) before it gets stored to my asset source at Amazon S3?

To complicate things a bit, I am uploading multiple files per entry, each one having its own field. One is the produced file (field name mp3File) selection) and the others are the raw recordings (field name rawFiles).

It is produced file (mp3File) that I am wanting to push to SoundCloud with their API.

I have a handle on how the SoundCloud API works, what I need is to pass the file path to the SoundCloud API. I think it is probably better to do this while the file still resides on the server before it makes its way to Amazon. Or it could be that I don't understand the flow here at all.

**Tags:** plugin-development, assets, amazon

---

## 1 answers

### Answer by username_0 on 2016-04-18. Score: 1 | Accepted answer

So this works. Now that I have the understanding of how things fire I can clean this up. I am open to better solutions.
    craft()->on('entries.onSaveEntry', function(Event $event)
    {   

        $entry = $event->params['entry'];    
        if($event->params['isNewEntry'] && $entry->getSection() == 'Episode')

        {

        require_once(craft()->path->getPluginsPath().'cranberry/vendor/soundcloud/autoload.php');

        $clientID       =   'xxxxxxxxxxxxxxx';
        $clientSecret   =   'xxxxxxx';
        $username       =   'xxxx';
        $password       =   'xxxx';

        $facade = new \Njasm\Soundcloud\SoundcloudFacade($clientID, $clientSecret);
        $facade->userCredentials($username, $password); 

        $mp3File = $entry->mp3File->first()->filename;
        $url = $entry->mp3File->first()->getUrl();

        $mp3TempFile = "/var/www/pathway/craft/storage/tmp/".$mp3File;

        // download the file from amazon s3 and push it to soundcloud
        $fp = file_put_contents($mp3TempFile, fopen($url, 'r'));

        $params = array(
            'track[title]' => iconv('UTF-8', 'ASCII//TRANSLIT', $entry->title),
            'track[sharing]' => 'public',
            //'track[asset_data]' => $mp3Filepath,
            'track[description]' => iconv('UTF-8', 'ASCII//TRANSLIT', strip_tags($entry->summary)),
            'track[track_type]' => 'podcast',
            'track[genre]' => 'Learning',
            'track[license]' => 'all-rights-reserved'
            );

        $response = $facade->upload($mp3TempFile, $params);
        $fp = unlink($mp3TempFile);

        } 

    });

> **Comments:**
> **username_0**: BTW, if anyone knows a craft path that gets me here /var/www/pathway/craft/storage, (something similar to craft()->path->getPluginsPath()) that would be helpful.  
> **username_1**: The [``CRAFT_STORAGE_PATH``](https://craftcms.com/docs/php-constants#path-constants)  constant should do the trick?  

---

