krizalys / onedrive-php-sdk

OneDrive SDK for PHP
BSD 3-Clause "New" or "Revised" License
144 stars 66 forks source link

[Support] 503 Service Unavailable #73

Closed solepixel closed 4 years ago

solepixel commented 4 years ago

So, in a different ticket, I got some sample code for handling file uploads. One of our users is experiencing issues uploading a 114MB file, with the error log reporting:

Server error: `PUT https://domainname-my.sharepoint.com/personal/accountname/_api/v2.0/drives/REDACTED/items/REDACTED/uploadSession?guid='1a4dc0c6-10e1-4ba7-b5a5-8adf10cb4ce8'&path='~tmpDC_backup-domainname-2019_12_06-11_11am-full-h3n9rtr9a3.zip'&overwrite=True&rename=False&dc=0&tempauth=REDACTED|some really long string of text|REDACTED` resulted in a `503 Service Unavailable` response:
{"error":{"code":"serviceNotAvailable","message":"Service unavailable","retryAfterSeconds":120}}

Any idea what could be the cause of this? 114MB hardly seems large enough to be causing the issue, although smaller files have successfully uploaded. Could there be server limitations? Where do I need to look to pinpoint this problem?

Somewhat of a side question, once startUpload() is called, is there a way to stop/cancel the transfer?

solepixel commented 4 years ago

Just a side note on this, I made a duplicate of the site running PHP v7.0.33 and was able to successfully send the 114MB file to OneDrive, so not really sure where the problem lies. The site was running PHP 7.3.11 and hosted at DreamHost.

krizalys commented 4 years ago

Thanks for your report @solepixel, I am currently on vacation, I will look into that in January. Thanks.

solepixel commented 4 years ago

@krizalys Thanks for checking-in! I'll let you know if I find anything new in the meantime.

krizalys commented 4 years ago

@solepixel The API server reported a URL with some GET parameters encoded strangely (eg. wrapped in single quotes), please can you share the code which is causing this error?

solepixel commented 4 years ago

@krizalys Sorry about the delayed feedback on this. This is the code being used:

$file_size = filesize( $file );
$file_info = pathinfo( $file );
$file_ext  = $file_info['extension'];

if ( 'zip' === $file_ext ) {
    $mime_type = 'application/zip';
} elseif ( 'php' === $file_ext ) {
    $mime_type = 'application/x-httpd-php';
} else {
    $mime_type = '';
}

$folder = self::get_drive_item( false, $folder_id );

if ( ! is_object( $folder ) ) {
    self::error( __( 'There was an error uploading file to OneDrive.', 'LION' ) );
    return false;
}

try {
    $args = array();
    if ( $mime_type ) {
        $args['contentType'] = $mime_type;
    }

    // Returns UploadSessionProxy object.
    $upload = $folder->startUpload( basename( $file ), fopen( $file, 'rb' ), $args );
} catch ( \Exception $e ) {
    self::error( __( 'Error', 'LION' ) . ' #201910150859: ' . __( 'Could not initiate upload for', 'LION' ) . ' `' . basename( $file ) . '`. ' . __( 'Details', 'LION' ) . ': ' . $e->getMessage() );
    return false;
}

try {
    // Potentially returns DriveItemProxy object.
    $status = $upload->complete();
} catch ( \Exception $e ) {
    self::error( __( 'Error', 'LION' ) . ' #201910161524: ' . __( 'OneDrive upload failed for', 'LION' ) . ' `' . basename( $file ) . '`. ' . __( 'Details', 'LION' ) . ': ' . $e->getMessage() );
    return false;
}
mgardener commented 4 years ago

@solepixel Did you find a solution for this problem? I am experiencing same problem while uploading 12MB CSV file.

mgardener commented 4 years ago

I have changed fopen parameter from "rb" to "r". It worked for me.

krizalys commented 4 years ago

@solepixel @mgardener I was able to successfully upload a 150 MB file using the code above + missing functions.

Is it still happening? If not, and since it's a 5XX error, I'll suggest that there is indeed a possiblity that the OneDrive service/your network was having issues at the time you experienced this.

Please feel free to reopen + PHP version, full code, upload file if you can reproduce this.

solepixel commented 4 years ago

I have applied the change @mgardener posted and I haven't heard any negative feedback, so I also am assuming that resolved the issue. Thanks @krizalys!